Hello,
I have one table in Access with 6 different fields. I would like the user to query each of these fields from a userform in Excel.
For example, I have 6 fields:LastName, Firstname, Address, Age, Gender, Email. I've created 6 different text boxes for each of these fields on a userform in Excel. I also have a Checkbox that determines if the search criteria is exact or "like".
I know how to search a single field (see code below) but I'm not sure how to make this work with multiple fields? How do I know which field the user is using for searchng? Or how many SQL statements do I need? Is it possible to search by both FirstName and Age.
Please suggest how to accomlish this,
Thank you very much,
Mila
Code example:
Here is the entire routine:
I have one table in Access with 6 different fields. I would like the user to query each of these fields from a userform in Excel.
For example, I have 6 fields:LastName, Firstname, Address, Age, Gender, Email. I've created 6 different text boxes for each of these fields on a userform in Excel. I also have a Checkbox that determines if the search criteria is exact or "like".
I know how to search a single field (see code below) but I'm not sure how to make this work with multiple fields? How do I know which field the user is using for searchng? Or how many SQL statements do I need? Is it possible to search by both FirstName and Age.
Please suggest how to accomlish this,
Thank you very much,
Mila
Code example:
Code:
Dim var
'set the search variable
var = Me.txtSearch
If CheckBox1 = True Then
SQL = "SELECT * FROM PhoneList WHERE LastName = '" & var & "'"
Else
SQL = "SELECT * FROM PhoneList WHERE LastName LIKE '" & var & "%" & "'"
End If
Code:
Private Sub cmdImport_Click()
'Declaring the necessary variables.
Dim cnn As ADODB.Connection 'dim the ADO collection class
Dim rs As ADODB.Recordset 'dim the ADO recordset class
Dim dbPath As String
Dim SQL As String
Dim i As Integer
Dim var
'add error handling
On Error GoTo errHandler:
'Disable screen flickering.
Application.ScreenUpdating = False
'get the path to the database
dbPath = Sheet1.Range("I3").Value
'set the search variable
var = Me.txtSearch
Set cnn = New ADODB.Connection ' Initialise the collection class variable
'Connection class is equipped with a —method— named Open
'—-4 aguments—- ConnectionString, UserID, Password, Options
'ConnectionString formula—-Key1=Value1;Key2=Value2;Key_n=Value_n;
cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & dbPath
'Create the SQL statement to retrieve the data from table.
If CheckBox1 = True Then
SQL = "SELECT * FROM PhoneList WHERE SURNAME = '" & var & "'"
Else
SQL = "SELECT * FROM PhoneList WHERE SURNAME LIKE '" & var & "%" & "'"
End If
'Create the ADODB recordset object.
Set rs = New ADODB.Recordset 'assign memory to the recordset
'ConnectionString Open '—-5 aguments—-
'Source, ActiveConnection, CursorType, LockType, Options
rs.Open SQL, cnn
'Check if the recordset is empty.
If rs.EOF And rs.BOF Then
'Close the recordet and the connection.
rs.Close
cnn.Close
'clear memory
Set rs = Nothing
Set cnn = Nothing
'Enable the screen.
Application.ScreenUpdating = True
'In case of an empty recordset display an error.
MsgBox "There are no records in the recordset!", vbCritical, "No Records"
Me.lstDataAccess.RowSource = ""
Exit Sub
End If
'Write the reocrdset values in the sheet.
Sheet2.Range("A2").CopyFromRecordset rs
'Close the recordset and the connection.
rs.Close
cnn.Close
'clear memory
Set rs = Nothing
Set cnn = Nothing
'Enable the screen.
Application.ScreenUpdating = True
Me.lstDataAccess.RowSource = "DataAccess"
'Inform the user that the macro was executed successfully.
MsgBox "Congratulation the data has been successfully Imported", vbInformation, "Import successful"
'error handler
On Error GoTo 0
Exit Sub
errHandler:
'clear memory
Set rs = Nothing
Set cnn = Nothing
MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure Import_Data"
End Sub