visual basic 6 access 2007 database programming cascading combo boxes - vb6

I have a table named: schoolInfo in access 2007 and it has two fields (schName and mjrName).
Now I'm trying to design a combo in Visual Basic 6 (cboMajors) which is related to the other combo (cboSchool).
As a matter of fact I want to have to cascading combo boxes. When I choose an Item in cboSchool the other combo should represents just the related majors for that school (records with schName=x and mjrName=y).
Private Sub Form_Activate()
connection
' the Connection is a code in module contains codes are needed to make the connection between form and the database
fill_schools
fill_majors
End Sub
Also,
Private Sub fill_schools()
With rs
.Open "select DISTINCT schName from tblSchoolsInfo", cn, 2, 3
Do While Not .EOF
cboSchool.AddItem (.Fields(0))
.MoveNext
Loop
End With
rs.Close
End Sub
Private Sub fill_majors()
With rs
.Open "select DISTINCT mjrName from tblSchoolsInfo where schName= '" & Me.cboSchool & " '", cn, 2, 3
Do While Not .EOF
cboMajors.AddItem (.Fields(0))
.MoveNext
Loop
End With
End Sub
Now: the first combo get correct values but the second one is completely empty.

In the snippet of code you have given us, I can't see anywhere where you actually select the school in Form_Activate(). This means that by the end of that procedure, there will be no selection in school, so fill_majors() will execute:
select DISTINCT mjrName from tblSchoolsInfo where schName= ' '
Incidentally, is that trailing space deliberate? In which case, this won't return records even if a school is selected.

The OP solved this in dreamincode.net. He was tacking an extra space on the end of his combo box string: Me.cboSchool & " '"
I've always wanted to say this: "This behavior is by design." :)

just a suggestion did you check the
cboMajors.AddItem (.Fields(0)) <--- .Fields()

Related

Excel Data Validation (with or without vba)

I have a column of cells eg "column C" whereby I would like to input a data validation.
I would like to restrict users to key in either "Done", "NA" or "any dates eg 01/10/2016, 31/03/2013 etc"
How do I create the above data validation? I am alright with using either Excel's "Data validation" function or VBA.
Thanks
Pretty simple bit of code here you could make to do whatever, highlight the cell or something, this just sets it back to blank. I would also desensitize them to caps and add some other options like "n/a"
Private sub worksheet_change(byval target as range)
If Target.Value <> "Done" And Target.Value <> "NA" And Not IsDate(Target.Value) Then
MsgBox ("incorrect entry, enter only blah or blah...")
Target.Value = ""
End If
End Sub

How to limit the value of the textbox in vb 6.0

I am doing a marks updating database system. I need to limit each of my textbox to be in the value of less than 100, when its over than 100 or when its not number, a message box will pop up and the data won't be save until the user change the mistake. How can I do it?
I agree with Hiren Pandya, but I thought I would add my own take as well.
Be aware that converting a string to a numerical value is not trivial, but the Val, CInt, CDBl etc. functions in VB6 can all give you behavior that close to what you want. (some of those links are for VB.Net, but can still be valuable). You want to make sure you are thinking about digit grouping, positive/negative, decimal separators, etc. when you are validating user input on your own. Most of the time, the built-in functions are good enough.
Private Sub Text1_Change()
On Error GoTo Err_Handler
Dim text As String
text = Text1.text
If IsNumeric(text) = True Then
'If you only want integers...
Dim value As Integer
value = Val(text)
If value <= 100 And value > 0 Then
'The value is good so whatever stuff you need to do
'And then leave the procedure
Exit Sub
End If
End If
'Let everything else fall through here...
Err_Handler:
MsgBox "Invalid input."
'Other stuff to prevent saving
End Sub
In the properties of the text box, set MaxLength to 2.
If you want a message, in the text box Change event, you could do...
If Len(txtBox.Text)>2 then msgbox...
then add your message in the messagebox.
I could go into more detail if you need it. Some thing like below...
Private Sub Text1_Change()
If Len(Text1) > 6 Then
Text1 = " "
MsgBox "Not more than six"
Text1.SetFocus
End If
End Sub

Searching Data from DataGrid Control using ADODC in VB6.0

I'm a student doing my final year mini project and am facing a problem related to searching data in the datagrid.
The error I'm getting is :
Run-time error : '3001'
Arguments are of wront type, are out of acceptable range, or are in conflict with one another
The code is :
Private Sub Command1_Click()
Dim item As String
Adodc1.Recordset.MoveFirst
item = Text1.Text
Adodc1.Recordset.Find "L_No = " & item
If Adodc1.Recordset.EOF Then
MsgBox "Record Set not found"
End If
End Sub
The above code is working when the data I'm searching is only number.
For example
When I search the data on the basis of L_Id which is a License ID an Integer value the searching is done and I'm getting the result.
When I search the data on the basis of L_No which is a License Number a string value which consists of both numbers and alphabets I'm getting the above error.
Do I have to parse the value is text1.text or do anything else?
When the field you're searching in is not a numeric type, you'll want to delimit it with single quotes:
Adodc1.Recordset.Find "L_No = '" & item & "'"

need help with ADO in VB6

I am making a POS system for a friend of mine, real small and simple.
Pretty much everything is coded right now except and inventory view
I am trying to make it so when it clicks a button it will load up into a listview
the UPC codes and the name associated with that UPC.
I am new to programming and I am trying to do this myself. I know I need to get a number of items in the database (how many UPC's) and then do a loop adding info in a listview.
but I am having trouble getting how many lines are in the database to start and end a loop
This would be easier if you posted some of your code, so we could see what you are starting with.
However, you should be able to do something like THIS (My VB/ADO is rusty, so I might blow the syntax a little. As Chris notes above, VB6 is getting a little long in the tooth . . .). In most cases, you should not need to know how many records are returned in order to populate your listView - Just use a "Do Until " Loop As follows:
Public Sub LoadListview()
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim SQL As String
SQL = _
"SELECT . . . " & _
"FROM . . . " & _
"WHERE . . . "
Set cn = New ADODB.Connection
With cn
.Provider = "Microsoft.Jet.OLEDB.4.0"
.ConnectionString = "YourConnectionString"
.Open
End With
Set rs = New ADODB.Recordset
rs.Open SQL, cn, adOpenForwardOnly, adLockReadOnly
With rs
If Not .EOF Then
Do Until .EOF
' Your code to populate your ListView Here
.MoveNext
Loop
End If
End With
rs.Close
cn.Close
Set rs = Nothing
Set cn = Nothing
End Sub
Note that I have simplified things here a little. in reality, if you are passing criteria into the WHERE clause, you should generally use PARAMETERS in conjunction with an ADODB.Command Object. I did not include an ADODB.Command in this example.

What's the most efficient way for accessing a single record in an ADO recordset?

I want to access individual records in a classic ADO recordset without enumerating over the entire recordset using .MoveNext. I'm aware of using AbsolutePosition as well as .Filter =. What's the best way?
I'm likely going to be accessing the recordset several times pulling out individual records that match a list of records in a particular field. For example, I have a recordset with records that have field values ranging from 1 to 100, I might have a separate array containing just {34, 64, 72}, and I want to do something to only the records in the recordset whose IDs are contained in the array.
If you are using server-side cursors, then the best method depends on the underlying OLE DB provider that you are using. It is quite possible that each access of the record could result in another trip to the server to read the data.
If you can use a client-side cursor, then I suspect that AbsolutePosition will be the best method to move to each record repeatedly. I believe that using a filter with a client-side cursor would require that it spin through each record matching the filter condition.
I ended up rewriting my answer due to new information, so:
My suggestion is to set the Filter property to what you want, then enumerate through the resulting subset and assign the Bookmark value of each record in the subset to a variable that you can easily match up with the IDs (so you might want to put them in an array in the order that their IDs are in the ID array you mention).
Use the Filter function on the Recordset object.
rs.Filter = "ID = '" & strID & "'"
I'm using this function all the time
Public Function InitIndexCollection( _
rs As Recordset, _
sFld As String, _
Optional sFld2 As String, _
Optional sFld3 As String, _
Optional ByVal HasDuplicates As Boolean) As Collection
Const FUNC_NAME As String = "InitIndexCollection"
Dim oFld As ADODB.Field
Dim oFld2 As ADODB.Field
Dim oFld3 As ADODB.Field
On Error GoTo EH
Set InitIndexCollection = New Collection
If Not IsRecordsetEmpty(rs) Then
Set oFld = rs.Fields(sFld)
If LenB(sFld2) <> 0 Then
Set oFld2 = rs.Fields(sFld2)
End If
If LenB(sFld3) <> 0 Then
Set oFld3 = rs.Fields(sFld3)
End If
If HasDuplicates Then
On Error Resume Next
End If
With rs
If oFld2 Is Nothing Then
.MoveFirst
Do While Not .EOF
InitIndexCollection.Add .Bookmark, C_Str(oFld.Value)
.MoveNext
Loop
ElseIf oFld3 Is Nothing Then
.MoveFirst
Do While Not .EOF
InitIndexCollection.Add .Bookmark, C_Str(oFld.Value) & "#" & C_Str(oFld2.Value)
.MoveNext
Loop
Else
.MoveFirst
Do While Not .EOF
InitIndexCollection.Add .Bookmark, C_Str(oFld.Value) & "#" & C_Str(oFld2.Value) & "#" & C_Str(oFld3.Value)
.MoveNext
Loop
End If
End With
End If
Exit Function
EH:
RaiseError FUNC_NAME
Resume Next
End Function

Resources