what is the coding format to display the previous and the next records using the F8 and F9 keyboard keys? (1 Viewer)

joko

New member
Local time
Today, 12:27
Joined
Apr 6, 2022
Messages
9
I have a sales form based on the sales invoice number with the name label2 and the list of items will go to the list

after there is a payment then I press enter it will switch to the next invoice number automatically.

I'm having trouble going back or calling to the previous record

what I want to ask is
I want to use the F8 and F9 keyboard keys to return to the previous and next record

what is the coding format?

Please help
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 12:27
Joined
May 7, 2009
Messages
16,775
you add code to the KeyDown event of your Form.
but before that make sure to Set the Key Preview property of the Form to Yes.
Code:
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyF8 Then
    'goto previous record
    DoCmd.GoToRecord , , acPrevious
    
ElseIf KeyCode = vbKeyF9 Then
    'goto next record
    DoCmd.GoToRecord , , acNext

End If
End Sub
 

joko

New member
Local time
Today, 12:27
Joined
Apr 6, 2022
Messages
9
you add code to the KeyDown event of your Form.
but before that make sure to Set the Key Preview property of the Form to Yes.
Code:
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyF8 Then
    'goto previous record
    DoCmd.GoToRecord , , acPrevious
   
ElseIf KeyCode = vbKeyF9 Then
    'goto next record
    DoCmd.GoToRecord , , acNext

End If
End Sub
but before that i already have code like below

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 113 Then Text1.SetFocus
If KeyCode = 114 Then List4.SetFocus
If KeyCode = 115 Then
If cn.State = adStateOpen Then cn.Close
If rs.State = adStateOpen Then rs.Close

Set cn = CurrentProject.AccessConnection
sql = "delete from jualdetail where nofaktur_jual='" & Label2.Caption & "' and barcode = '" & List4.Column(0) & "'"
cn.Execute (sql)
tampil
total_jual
End If
If KeyCode = 116 Then Text26.SetFocus

End Sub

where do i put it
 

Gasman

Enthusiastic Amateur
Local time
Today, 05:27
Joined
Sep 21, 2011
Messages
10,838
Might want to research Select Case statement?
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 12:27
Joined
May 7, 2009
Messages
16,775
just replace your keydown code with this one (already cleaned)
Code:
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
    Case Is = 113
        Text1.SetFocus
    Case Is = 114
        List4.SetFocus
    Case Is = 115
        If cn.State = adStateOpen Then cn.Close
        If rs.State = adStateOpen Then rs.Close

        Set cn = CurrentProject.AccessConnection
        SQL = "delete from jualdetail where nofaktur_jual='" & Label2.Caption & "' and barcode = '" & List4.Column(0) & "'"
        cn.Execute (SQL)
        tampil
        total_jual
    Case Is = 116
        Text26.SetFocus

    Case Is = vbKeyF8
        'goto previous record
        DoCmd.GoToRecord , , acPrevious
    
    Case Is = vbKeyF9
        'goto next record
        DoCmd.GoToRecord , , acNext

End Select
End Sub
 

joko

New member
Local time
Today, 12:27
Joined
Apr 6, 2022
Messages
9
just replace your keydown code with this one (already cleaned)
Code:
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
    Case Is = 113
        Text1.SetFocus
    Case Is = 114
        List4.SetFocus
    Case Is = 115
        If cn.State = adStateOpen Then cn.Close
        If rs.State = adStateOpen Then rs.Close

        Set cn = CurrentProject.AccessConnection
        SQL = "delete from jualdetail where nofaktur_jual='" & Label2.Caption & "' and barcode = '" & List4.Column(0) & "'"
        cn.Execute (SQL)
        tampil
        total_jual
    Case Is = 116
        Text26.SetFocus

    Case Is = vbKeyF8
        'goto previous record
        DoCmd.GoToRecord , , acPrevious
   
    Case Is = vbKeyF9
        'goto next record
        DoCmd.GoToRecord , , acNext

End Select
End Sub
you can't go to the specified record, run time error 2105
 

Gasman

Enthusiastic Amateur
Local time
Today, 05:27
Joined
Sep 21, 2011
Messages
10,838
Have you checked that you are not on first or last record and then trying previous or next record?

 
Last edited:

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 12:27
Joined
May 7, 2009
Messages
16,775
you add error handler:
Code:
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
On Error Goto Err_Handler
Select Case KeyCode
    Case Is = 113
        Text1.SetFocus
    Case Is = 114
        List4.SetFocus
    Case Is = 115
        If cn.State = adStateOpen Then cn.Close
        If rs.State = adStateOpen Then rs.Close

        Set cn = CurrentProject.AccessConnection
        SQL = "delete from jualdetail where nofaktur_jual='" & Label2.Caption & "' and barcode = '" & List4.Column(0) & "'"
        cn.Execute (SQL)
        tampil
        total_jual
    Case Is = 116
        Text26.SetFocus

    Case Is = vbKeyF8
        'goto previous record
        DoCmd.GoToRecord , , acPrevious
    
    Case Is = vbKeyF9
        'goto next record
        DoCmd.GoToRecord , , acNext

End Select

Exit_Sub:
Exit Sub
Err_Handler:
    If Err <> 2105 Then
        MsgBox Err.Number & ": " & Err.Description
    End If
    Resume Exit_Sub
End Sub
 

joko

New member
Local time
Today, 12:27
Joined
Apr 6, 2022
Messages
9
you add error handler:
Code:
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
On Error Goto Err_Handler
Select Case KeyCode
    Case Is = 113
        Text1.SetFocus
    Case Is = 114
        List4.SetFocus
    Case Is = 115
        If cn.State = adStateOpen Then cn.Close
        If rs.State = adStateOpen Then rs.Close

        Set cn = CurrentProject.AccessConnection
        SQL = "delete from jualdetail where nofaktur_jual='" & Label2.Caption & "' and barcode = '" & List4.Column(0) & "'"
        cn.Execute (SQL)
        tampil
        total_jual
    Case Is = 116
        Text26.SetFocus

    Case Is = vbKeyF8
        'goto previous record
        DoCmd.GoToRecord , , acPrevious
   
    Case Is = vbKeyF9
        'goto next record
        DoCmd.GoToRecord , , acNext

End Select

Exit_Sub:
Exit Sub
Err_Handler:
    If Err <> 2105 Then
        MsgBox Err.Number & ": " & Err.Description
    End If
    Resume Exit_Sub
End Sub
I have done it, the result does not affect or there is no reaction
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 12:27
Joined
May 7, 2009
Messages
16,775
there will be no Reaction if your form is set as Data Entry
or you have no record yet on your table.
 

joko

New member
Local time
Today, 12:27
Joined
Apr 6, 2022
Messages
9
there will be no Reaction if your form is set as Data Entry
or you have no record yet on your tab
you add error handler:
Code:
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
On Error Goto Err_Handler
Select Case KeyCode
    Case Is = 113
        Text1.SetFocus
    Case Is = 114
        List4.SetFocus
    Case Is = 115
        If cn.State = adStateOpen Then cn.Close
        If rs.State = adStateOpen Then rs.Close

        Set cn = CurrentProject.AccessConnection
        SQL = "delete from jualdetail where nofaktur_jual='" & Label2.Caption & "' and barcode = '" & List4.Column(0) & "'"
        cn.Execute (SQL)
        tampil
        total_jual
    Case Is = 116
        Text26.SetFocus

    Case Is = vbKeyF8
        'goto previous record
        DoCmd.GoToRecord , , acPrevious

    Case Is = vbKeyF9
        'goto next record
        DoCmd.GoToRecord , , acNext

End Select

Exit_Sub:
Exit Sub
Err_Handler:
    If Err <> 2105 Then
        MsgBox Err.Number & ": " & Err.Description
    End If
    Resume Exit_Sub
End Sub
s

there will be no Reaction if your form is set as Data Entry
or you have no record yet on your table.

there will be no Reaction if your form is set as Data Entry
or you have no record yet on your table.
Thank you sir for all the explanations, I salute your patience in guiding me, there is one more thing I want to ask, I hope you are pleased to answer it.

I have a combobox (combo6) it contains data on the name of the item, and I also have a text box (text1) that contains the code for the item, when I type only 1 letter of data from the item name in combo6, combo6 immediately appears item data according to the letter the first thing I typed, it's just that the text1 doesn't immediately appear the code from the item. the item code can only appear if I use my mouse then I click the arrow then look for the item data then I click then the item code appears in text1. this way makes me slow

my wish is that as soon as I type the name of the item in the combobox, the item name appears immediately (and this is exactly what I wanted), and the text1 also appears according to the code of the item and once everything is in order both in combo6 and text1 then when I press enter the data goes directly to the list.

can you help

text1 already has coding as below, only I added a combobox to make it easier to find item names

this is coding text1

Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 13 Then
c_barcode = Text1
If cn.State = adStateOpen Then cn.Close
If rs.State = adStateOpen Then rs.Close

Set cn = CurrentProject.AccessConnection
sql = "Select * from jual where nofaktur_jual='" & Text2 & "'"
rs.Open sql, cn, adOpenKeyset
If rs.EOF Then
sql = "insert into jual (nofaktur_jual,tgl_jual,idkasir)values (" & _
"'" & Text2 & "','" & Date & "','" & id_kasir & "')"
cn.Execute (sql)
End If

If cn.State = adStateOpen Then cn.Close
If rs.State = adStateOpen Then rs.Close
Set cn = CurrentProject.AccessConnection

If Left(Text1.Text, 1) = "#" Then
sql = "update jualdetail set qty_jual='" & Split(Text1.Text, "#")(1) & "' where nofaktur_jual='" & Text2 & "' and barcode='" & c + Barcode & "'"
cn.Execute (sql)
Else
sql = "Select * from jualdetail where nofaktur_jual='" & Text2 & "' and barcode='" & Text1.Text & "'"
rs.Open sql, cn, adOpenKeyset
If rs.EOF Then
sql = " insert into jualdetail (nofaktur_jual,barcode,qty_jual)values (" & _
"'" & Text2 & "','" & Text1.Text & "',1)"
cn.Execute (sql)
Else
sql = "update jualdetail set qty_jual=qty_jual+1 where nofaktur_jual='" & Text2 & "' and barcode='" & Text1.Text & "'"
cn.Execute (sql)
End If
End If
tampil
total_jual
c_barcode = Text1.Text
Text1 = ""
Text1.SetFocus
End If
End Sub
 
Last edited:

Gasman

Enthusiastic Amateur
Local time
Today, 05:27
Joined
Sep 21, 2011
Messages
10,838
I cannot help with your query, but do yourself a favour early on. Give your controls some meaningful names. If you come back 3 months later to make amendments, Text1 is not going to mean a thing to you or anyone else that might have to work with the code.
 

Users who are viewing this thread

Top Bottom