Msjunior90
09-18-2014, 02:26 AM
Hi,
I'm having a problem that I would like to have help in. I am developing a userform in Word, where I have two Combobox. Both Comboboxes reads data information from an Access Database 2010 table. In my Table I have a Field with different names, and in the other Field I have undersubject to the first Field.
It looks like this.
Filed1 Field2
CompanyA Toys
CarToys
Dolls
So the meaning of the table is that Field2 shpuld be connected to Field 1. Now my code that reads from the databse looks like this
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim NoOfRecords As Long
Set db = OpenDatabase("C:\Users\company.accdb")
'Define the first recordset
Set rs = db.OpenRecordset("SELECT Field FROM FieldTable")
'Determine the number of records in the recordset
With rs
.MoveLast
NoOfRecords = .RecordCount
.MoveFirst
End With
cboField.ColumnCount = rs.Fields.Count
'Load the ListBox with the retrieved records
cboField.Column = rs.GetRows(NoOfRecords)
'Cleanup
rs.Close
db.Close
Set rs = Nothing
Set db = Nothing
lbl_Exit:
Exit Sub
It reads everything from Field one and itworks!
Now the problem is when I'm trying to read from the other field, so if CompanyA is selected in combobox1 I want Toys, cars dolls to show up in combobox2. And it doesn't, all that is showing up is only Toys and not the rest. What am I doing wrong, please help me :/
The second code looks like this
Private Sub cboField_Change()
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim NoOfRecords As Long
Dim strReport As String
cboReport.Clear
'Open the .accdb form database to retrieve data
Set db = OpenDatabase("C:\Users\company.accdb")
'Define the first recordset
Set rs = db.OpenRecordset("SELECT Toys FROM FieldTable WHERE Field ='" & cboField & "'")
'Determine the number of records in the recordset
With rs
.MoveLast
NoOfRecords = .RecordCount
.MoveFirst
End With
'Set the number of ListBox columns = number of fields in the recordset
cboReport.ColumnCount = rs.Fields.Count
'Load the ListBox with the retrieved records
cboReport.Column = rs.GetRows(NoOfRecords)
'Cleanup
rs.Close
db.Close
Set rs = Nothing
Set db = Nothing
lbl_Exit:
Exit Sub
End Sub
I'm having a problem that I would like to have help in. I am developing a userform in Word, where I have two Combobox. Both Comboboxes reads data information from an Access Database 2010 table. In my Table I have a Field with different names, and in the other Field I have undersubject to the first Field.
It looks like this.
Filed1 Field2
CompanyA Toys
CarToys
Dolls
So the meaning of the table is that Field2 shpuld be connected to Field 1. Now my code that reads from the databse looks like this
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim NoOfRecords As Long
Set db = OpenDatabase("C:\Users\company.accdb")
'Define the first recordset
Set rs = db.OpenRecordset("SELECT Field FROM FieldTable")
'Determine the number of records in the recordset
With rs
.MoveLast
NoOfRecords = .RecordCount
.MoveFirst
End With
cboField.ColumnCount = rs.Fields.Count
'Load the ListBox with the retrieved records
cboField.Column = rs.GetRows(NoOfRecords)
'Cleanup
rs.Close
db.Close
Set rs = Nothing
Set db = Nothing
lbl_Exit:
Exit Sub
It reads everything from Field one and itworks!
Now the problem is when I'm trying to read from the other field, so if CompanyA is selected in combobox1 I want Toys, cars dolls to show up in combobox2. And it doesn't, all that is showing up is only Toys and not the rest. What am I doing wrong, please help me :/
The second code looks like this
Private Sub cboField_Change()
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim NoOfRecords As Long
Dim strReport As String
cboReport.Clear
'Open the .accdb form database to retrieve data
Set db = OpenDatabase("C:\Users\company.accdb")
'Define the first recordset
Set rs = db.OpenRecordset("SELECT Toys FROM FieldTable WHERE Field ='" & cboField & "'")
'Determine the number of records in the recordset
With rs
.MoveLast
NoOfRecords = .RecordCount
.MoveFirst
End With
'Set the number of ListBox columns = number of fields in the recordset
cboReport.ColumnCount = rs.Fields.Count
'Load the ListBox with the retrieved records
cboReport.Column = rs.GetRows(NoOfRecords)
'Cleanup
rs.Close
db.Close
Set rs = Nothing
Set db = Nothing
lbl_Exit:
Exit Sub
End Sub