M
Martin Cameron
Problem After a user selects a client's name from a combobox, I want to
select the addrss details from an excel speadsheet using DAO - someting like:
sqlStr="select * from ClientNames where name like '" & ComboBox.value & "'"
The excel spreadsheet has four columns: Name, Street, Suburb, City.
Here's the code I use to populate the ComboBox:
<code>
Dim docPath As String
Dim FName As String
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim NoOfRecords As Long
' Open the database
docPath = ThisDocument.Path
docPath = Environ("USERPROFILE") + "\My Documents"
FName = "test.xlsx"
Set db = OpenDatabase(docPath & "\" & FName, False, False, "Excel 8.0")
' Retrieve the recordset
Set rs = db.OpenRecordset("SELECT name FROM `ClientNames`") ' where name
like 'Martin Cameron'")
' Determine the number of retrieved records
With rs
.MoveLast
NoOfRecords = .RecordCount
.MoveFirst
End With
' Set the number of Columns = number of Fields in recordset
ClientNames.ColumnCount = rs.Fields.Count
' Load the ListBox with the retrieved records
ClientNames.Column = rs.GetRows(NoOfRecords)
' Cleanup
rs.Close
db.Close
Set rs = Nothing
Set db = Nothing
</code>
select the addrss details from an excel speadsheet using DAO - someting like:
sqlStr="select * from ClientNames where name like '" & ComboBox.value & "'"
The excel spreadsheet has four columns: Name, Street, Suburb, City.
Here's the code I use to populate the ComboBox:
<code>
Dim docPath As String
Dim FName As String
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim NoOfRecords As Long
' Open the database
docPath = ThisDocument.Path
docPath = Environ("USERPROFILE") + "\My Documents"
FName = "test.xlsx"
Set db = OpenDatabase(docPath & "\" & FName, False, False, "Excel 8.0")
' Retrieve the recordset
Set rs = db.OpenRecordset("SELECT name FROM `ClientNames`") ' where name
like 'Martin Cameron'")
' Determine the number of retrieved records
With rs
.MoveLast
NoOfRecords = .RecordCount
.MoveFirst
End With
' Set the number of Columns = number of Fields in recordset
ClientNames.ColumnCount = rs.Fields.Count
' Load the ListBox with the retrieved records
ClientNames.Column = rs.GetRows(NoOfRecords)
' Cleanup
rs.Close
db.Close
Set rs = Nothing
Set db = Nothing
</code>