-
I am using the following code to update a field in a DB. The only problem is I have to hit the key twice for it to work.
Code:
Private Sub txtService_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyDelete Then
UpdateNote
End If
End Sub
Private Sub txtService_KeyPress(KeyAscii As Integer)
If KeyAscii = 8 Then
UpdateNote
Else
KeyAscii = 0
End If
End Sub
Private Sub UpdateNote()
Dim strSQL As String
Dim oRS As Recordset
Dim strName As String
strName = txtCustName
strSQL = "SELECT ServiceNotes "
strSQL = strSQL & "FROM Locations "
strSQL = strSQL & "WHERE Location = '" & ListView1.SelectedItem & "' And Customer = '" & strName & "'"
Set oRS = New Recordset
oRS.Open strSQL, goConn, _
adOpenKeyset, adLockPessimistic, adCmdText
With oRS
!ServiceNotes = Str2Field(txtService)
End With
oRS.Update
oRS.Close
End Sub
How can I do this?
-
Why do you have it in both events? Only one event is necessary.
Choose whichever one you want, both work the same.
Set the Form's KeyPreview property to True.
And it should work.
-
Okay
Now I have it in one event. I started with the keypress, but couldn't figure out the Delete in that, so went to keydown.
Code:
Private Sub txtService_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyDelete Or vbKeyBack Then
UpdateNote
End If
End Sub
but I still need to hit it twice. If I wanted Gatess to say Gates, I have to backspace to Gate. If I highlight and hit delete once, nothing happens. If I hit it twice, it works. Let me clear this up. The textbox is doing as it should, it is the saved field that is wrong. If I change it in the textbox to "Gate" it will come back as "Gates" the next time I go to that item in the listview.
-
Got it.
Used KeyUp. That fixed it.