1,105,395 Community Members

Click A Command Button When "ENTER" Is Pressed In A Text Box?

Member Avatar
shena
Junior Poster in Training
58 posts since May 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi all,
I have two textboxes only in a form. At textbox1, upon pressing enter/carriage return it suppose to setfocus to textbox2 instead it's performing textbox2's keypress event by calling a command button. How to handle this? Where am i doing wrong? Please help.

Private Sub txtSNO_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then 'if ENTER or carriage return then move to next text box
    txtRMANo.SetFocus
End If
End Sub

Private Sub txtRMANo_KeyPress(KeyAscii As Integer)
If Chr(KeyAscii) = Chr(13) Then 'if ENTER or carriage return then call print command button
    Call cmdPrint_Click
End If
End Sub
Member Avatar
cguan_77
Nearly a Posting Virtuoso
1,379 posts since Apr 2007
Reputation Points: 8 [?]
Q&As Helped to Solve: 117 [?]
Skill Endorsements: 0 [?]
 
0
 

have you tried taking out the if else statement on txtRmano command button? try below:

Private Sub txtSNO_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then 'if ENTER or carriage return then move to next text box   
 txtRMANo.SetFocus
End If
End Sub

Private Sub txtRMANo_KeyPress(KeyAscii As Integer)
'If Chr(KeyAscii) = Chr(13) Then 'if ENTER or carriage return then call print command button
    Call cmdPrint_Click
'End If
End Sub
Member Avatar
cguan_77
Nearly a Posting Virtuoso
1,379 posts since Apr 2007
Reputation Points: 8 [?]
Q&As Helped to Solve: 117 [?]
Skill Endorsements: 0 [?]
 
0
 

actually, you want to programatically click the button or what?
or you can try this:

Private Sub txtSNO_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then 'if ENTER or carriage return then move to next text box    
'txtRMANo.SetFocus instead of set focus try this
'i assume that the button is under a form named form1

form1.controls("txtRmano").value = True

End If

End Sub
Member Avatar
robert_c_guy
Newbie Poster
4 posts since Dec 2008
Reputation Points: 12 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

Also, Have you set the KeyPreview property of the form to true? (in the properties dialog of the gui or directly in the *.frm file under "Begin VB.Form" set "KeyPreview = -1" for true) otherwise, I believe, the KeyPress events get digested before they can filter through to the handlers for individual objects on the form.

Member Avatar
AndreRet
Industrious Poster
4,492 posts since Jan 2008
Reputation Points: 362 [?]
Q&As Helped to Solve: 499 [?]
Skill Endorsements: 24 [?]
 
0
 

Call the command print by its value.

Private Sub txtRMANo_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then 'if ENTER or carriage return then call print command button
    cmdPrint.Value = True
End If
End Sub
Member Avatar
shena
Junior Poster in Training
58 posts since May 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi guys, thanks a lot for the replies. I found out the actual problem. It's due to the barcode scanner which scans and then executes "ENTER" twice (2X of carriage returns) at the first textbox (txtSNO). So, that's reason why it calls print command once i scan. Is there any way to stop such an execution? I would like to run the program logic as it is. How to accomplish this?

Member Avatar
AndreRet
Industrious Poster
4,492 posts since Jan 2008
Reputation Points: 362 [?]
Q&As Helped to Solve: 499 [?]
Skill Endorsements: 24 [?]
 
0
 

Why does it execute the print event twice? It must be in your code somewhere where you have a call to the print event as well as under your textbox change event.

Post us some code, let's have a look.:)

Member Avatar
shena
Junior Poster in Training
58 posts since May 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Not the print event, but "ENTER" twice.
This is the whole thing:

Private Sub cmdPrint_Click()
Dim sno As String

    If (Me.txtSNO.Text = "") Then
    MsgBox "Please scan the Serial Number!"
    txtSNO.SetFocus
    Exit Sub
    ElseIf (Me.txtRMANo.Text = "") Then
    MsgBox "Please enter RMA#!"
    txtRMANo.SetFocus
    Exit Sub
    End If
    
    sno = Me.txtSNO.Text
    
    strSql = "select * from MODEL_MASTER where PARTNUMBER like '" & sno & "'"
    Set rs = New ADODB.Recordset

    Call ConnectDB
    rs.Open strSql, DB, adOpenStatic, adLockOptimistic

    If rs.RecordCount <> 0 Then
        getPartNumber = rs.Fields!PartNumber
        getModel = rs.Fields!model_name
    Else
        MsgBox "The corresponding part number doesn't exist! ", vbInformation
        txtSNO.Text = ""
        txtSNO.SetFocus
        Set rs = Nothing
        Exit Sub
    End If
    
    Call Print_Label
    
End Sub

Sub Print_Label()

Dim serialNumber, RMANo As String

serialNumber = txtSNO.Text
RMANo = txtRMANo.Text

'Open the port:
Open "LPT1" For Output As #1

'Print directly to it:
    Print #1, "^XA^FO60,20^BY2^B3N,N,56,N,N^FD" & getPartNumber & "^FS^FO140,80^ADN,36,20^FD" & getPartNumber & _
              "^FS^FO15,127^BY2^B3N,N,56,N,N^FD" & serialNumber & "^FS^FO110,187^ADN,36,20^FD" & serialNumber & _
              "^FS^FO95,234^BY2^B3N,N,56,N,N^FD" & getModel & "^FS^FO135,296^ADN,36,20^FD" & getModel & _
              "^FS^FO121,341^BY2^B3N,N,56,N,N^FD" & RMANo & "^FS^FO241,401^ADN,36,20^FD" & RMANo & _
              "^FS^FO121,401^ADN,36,20^FDRMA" & "^FS^XZ"

'Close the port:
Close #1

Me.txtSNO.Text = ""
Me.txtRMANo.Text = ""

Me.txtSNO.SetFocus

End Sub

Private Sub txtSNO_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then 'if ENTER or carriage return then move to next text box
    txtRMANo.SetFocus
End If
End Sub

Private Sub txtRMANo_KeyPress(KeyAscii As Integer)
If Chr(KeyAscii) = Chr(13) Then 'if ENTER or carriage return then move to next text box
    Call cmdPrint_Click
End If
End Sub

Could it be that i need to remove one carriage return/line feed from the scanned value? Because i tried scanning at text file and the cursor jumped 2 lines. So, in my case the first carriage return focuses txtRMANo textbox and the second carriage return calls the cmdPrint_click i guess. Whenever i scan at txtSNO textbox, i get prompted with "Please enter RMA#" message as you can see in my code.

Member Avatar
cguan_77
Nearly a Posting Virtuoso
1,379 posts since Apr 2007
Reputation Points: 8 [?]
Q&As Helped to Solve: 117 [?]
Skill Endorsements: 0 [?]
 
0
 

hi shena, if your scanner scans and executes enter twice, i guess you might need to make some logic in your code. Please check code below but not so sure whether this one will work, but maybe you can get idea from this:

Private Sub txtRMANo_KeyPress(KeyAscii As Integer)
Dim i As Integer

If Chr(KeyAscii) = Chr(13) Then 'if ENTER or carriage return then move to next text box

i = i + 1 

If i = 1 then  'check if enter has been pressed once else don't execute print
    Call cmdPrint_Click
 Else
End If   

End If

End Sub
Member Avatar
AndreRet
Industrious Poster
4,492 posts since Jan 2008
Reputation Points: 362 [?]
Q&As Helped to Solve: 499 [?]
Skill Endorsements: 24 [?]
 
0
 

If rs.RecordCount <> 0 Then
getPartNumber = rs.Fields!PartNumber
getModel = rs.Fields!model_name
Else
MsgBox "The corresponding part number doesn't exist! ", vbInformation
txtSNO.Text = ""
txtSNO.SetFocus
Set rs = Nothing
Exit Sub
End If

Call Print_Label

Change the code above to this -

If rs.RecordCount <> 0 Then
        getPartNumber = rs.Fields!PartNumber
        getModel = rs.Fields!model_name

        Call Print_Label
        Exit Sub
    Else
        MsgBox "The corresponding part number doesn't exist! ", vbInformation
        txtSNO.Text = ""
        txtSNO.SetFocus
        Set rs = Nothing
        Exit Sub
    End If

Let me know if it worked.:)

Member Avatar
shena
Junior Poster in Training
58 posts since May 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi cguan, i tried adding the checking method, but no difference. It still calls the cmdPrint once i scan.

AndreRet, i changed the code and it just prints as usual and executes the "ENTER" twice.

Any idea guys?

Member Avatar
AndreRet
Industrious Poster
4,492 posts since Jan 2008
Reputation Points: 362 [?]
Q&As Helped to Solve: 499 [?]
Skill Endorsements: 24 [?]
 
0
 

I'll see if I can solve this for you tomorrow. WEEKEND YAHOOOO!!!:)

Member Avatar
AndreRet
Industrious Poster
4,492 posts since Jan 2008
Reputation Points: 362 [?]
Q&As Helped to Solve: 499 [?]
Skill Endorsements: 24 [?]
 
0
 

As a matter of interest, why are you using LPT1 port and not just "Printer.Print"?

Member Avatar
AndreRet
Industrious Poster
4,492 posts since Jan 2008
Reputation Points: 362 [?]
Q&As Helped to Solve: 499 [?]
Skill Endorsements: 24 [?]
 
0
 

Also try the code when you remove the "" from "LPT1"

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article