A
Andrew R
Hi all
I'm having an issue with a piece of code designed to populate a text
box as the user types into it.
The theory is as follows:
In the KeyDown procedure:
1) Check for backspace or delete and quit if they are pressed
2) Check if less than 3 characters entered and quit if so - not enough
data to look up yet.
3) Otherwise, create a recordset based on a Select statement which
compares what has been entered so far with the start of contact names
in the contacts table. Populate the textbox with the first match if
there is one, and position the cursor so that the user can continue
typing where they were.
All appears to work until the recordset is opened. The strSQL variable
I'm using as the source of the recordset holds this (the letters
entered were "Ang"):
--
Select strFirstName & ' ' & strLastName & ' (' & strCompanyName & ')'
FROM tblContacts inner join tblCompanies on
tblContacts.lngCompanyID=tblCompanies.lngCompanyid WHERE strFirstName &
' ' & strLastName & ' (' & strCompanyName & ')' LIKE 'anG*'
--
If I enter this in query design view as a sql statement, I get 2
results, which is what I expected. BUT, when I open the recordset in
the next line of code, .eof is always true!
I've tried opening the query written with the above sql statement
instead of generating the SQL on the fly as a test, but with the same
result -always .eof=true.
Please help!!
Andrew
Here's the code in full:
---
Dim rsMatch As ADODB.Recordset
Dim strSQL As String
Dim intCurrentPos As Integer
'Clear out the variable if the user presses delete or backspace
If KeyCode = vbKeyDelete Or KeyCode = vbKeyBack Then
strNameStart = ""
' and finish
GoTo ExitHere
Else
'if user presses something other than delete or backspace, add it to
the variable
strNameStart = txtClientName.Text & Chr(KeyCode)
'Do nothing if we don't have at least 3 characters
If Len(strNameStart) < 3 Then GoTo ExitHere
End If
'If we are still here, we must have 3 characters to analyse.
'Extract a list of all Contacts that match these three characters
'Set up the recordset
Set rsMatch = New ADODB.Recordset
rsMatch.ActiveConnection = CurrentProject.Connection
rsMatch.CursorType = adOpenDynamic
rsMatch.LockType = adLockOptimistic
'Create the SQL statement
strSQL = "Select strFirstName & ' ' & strLastName & ' (' &
strCompanyName & ')' " _
& "FROM tblContacts inner join tblCompanies on
tblContacts.lngCompanyID=" _
& "tblCompanies.lngCompanyid " _
& "WHERE strFirstName & ' ' & strLastName & ' (' &
strCompanyName & ')' " _
& "LIKE '" & strNameStart & "*'"
'Assign sql statement to the recordset
rsMatch.Source = strSQL
'Open
rsMatch.Open options:=adCmdText
If Not rsMatch.EOF Then
'Work out where to put the cursor
intCurrentPos = Len(strNameStart)
'Put the first match in the name box
txtClientName.Value = rsMatch(0)
'and position the cursor
txtClientName.SelStart = intCurrentPos
End If
'Clean up
rsMatch.Close
Set rsMatch = Nothing
ExitHere:
End Sub
I'm having an issue with a piece of code designed to populate a text
box as the user types into it.
The theory is as follows:
In the KeyDown procedure:
1) Check for backspace or delete and quit if they are pressed
2) Check if less than 3 characters entered and quit if so - not enough
data to look up yet.
3) Otherwise, create a recordset based on a Select statement which
compares what has been entered so far with the start of contact names
in the contacts table. Populate the textbox with the first match if
there is one, and position the cursor so that the user can continue
typing where they were.
All appears to work until the recordset is opened. The strSQL variable
I'm using as the source of the recordset holds this (the letters
entered were "Ang"):
--
Select strFirstName & ' ' & strLastName & ' (' & strCompanyName & ')'
FROM tblContacts inner join tblCompanies on
tblContacts.lngCompanyID=tblCompanies.lngCompanyid WHERE strFirstName &
' ' & strLastName & ' (' & strCompanyName & ')' LIKE 'anG*'
--
If I enter this in query design view as a sql statement, I get 2
results, which is what I expected. BUT, when I open the recordset in
the next line of code, .eof is always true!
I've tried opening the query written with the above sql statement
instead of generating the SQL on the fly as a test, but with the same
result -always .eof=true.
Please help!!
Andrew
Here's the code in full:
---
Dim rsMatch As ADODB.Recordset
Dim strSQL As String
Dim intCurrentPos As Integer
'Clear out the variable if the user presses delete or backspace
If KeyCode = vbKeyDelete Or KeyCode = vbKeyBack Then
strNameStart = ""
' and finish
GoTo ExitHere
Else
'if user presses something other than delete or backspace, add it to
the variable
strNameStart = txtClientName.Text & Chr(KeyCode)
'Do nothing if we don't have at least 3 characters
If Len(strNameStart) < 3 Then GoTo ExitHere
End If
'If we are still here, we must have 3 characters to analyse.
'Extract a list of all Contacts that match these three characters
'Set up the recordset
Set rsMatch = New ADODB.Recordset
rsMatch.ActiveConnection = CurrentProject.Connection
rsMatch.CursorType = adOpenDynamic
rsMatch.LockType = adLockOptimistic
'Create the SQL statement
strSQL = "Select strFirstName & ' ' & strLastName & ' (' &
strCompanyName & ')' " _
& "FROM tblContacts inner join tblCompanies on
tblContacts.lngCompanyID=" _
& "tblCompanies.lngCompanyid " _
& "WHERE strFirstName & ' ' & strLastName & ' (' &
strCompanyName & ')' " _
& "LIKE '" & strNameStart & "*'"
'Assign sql statement to the recordset
rsMatch.Source = strSQL
'Open
rsMatch.Open options:=adCmdText
If Not rsMatch.EOF Then
'Work out where to put the cursor
intCurrentPos = Len(strNameStart)
'Put the first match in the name box
txtClientName.Value = rsMatch(0)
'and position the cursor
txtClientName.SelStart = intCurrentPos
End If
'Clean up
rsMatch.Close
Set rsMatch = Nothing
ExitHere:
End Sub