Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sendkeys to click button not working...

Status
Not open for further replies.

lameid

Programmer
Jan 31, 2001
4,212
US
This is a generic procedure to allow a form to be opened and a value entered.

Because this is generic and I really do not want to write public functions to wrap the private event functions on the form, I am using setfocus and sendkeys to click buttons.

My first call works, the second does not.


Code:
Sub FormNav(strForm As String, varKey As Variant, strControlName As String)
    Dim frm As Form
    
    DoCmd.OpenForm strForm, acNormal
    
    Set frm = Forms(strForm)
    
    Select Case frm.Controls(strControlName).ControlType
    Case acListBox, acComboBox
        With frm.Controls(strControlName)
            If .LimitToList Then
                Dim RS As DAO.Recordset
                Dim strkey As String
                Dim strCriteriaValue As String
                Set RS = CurrentDb.OpenRecordset(.RowSource)
                strkey = RS.Fields(.BoundColumn - 1).Name
                Select Case RS.Fields(.BoundColumn - 1).Type
                Case dbMemo, dbChar, dbText
                    strCriteriaValue = """" & varKey & """"
                Case dbDate
                    strCriteriaValue = "#" & varKey & "#"
                Case Else
                    strCriteriaValue = varKey
                End Select
                If DCount(strkey, .RowSource, strkey & " = " & strCriteriaValue) = 0 Then
                    'Value does not exist in drop down
                    'Click New button to add new record
                    frm.Controls("btnNew").SetFocus
                    SendKeys ("{Enter}") 'Fires btnNew_Click on form
                    GoTo FormNav_Cleanup 'Skip looking up
                Else
                    'Continue
                End If
                
           End If
            .SetFocus
            .Text = varKey
            SendKeys ("{Tab}") 'can't call private after update events from another procedure, this or have to write public function to call private procedure.
            [red]
            'Click Edit button to edit existing record
            frm.Controls("btnEdit").SetFocus
            SendKeys ("{Enter}") 'does not seem to do anything
                                 'hitting enter after code runs, fires click event[/red]
        End With
    Case Else
        frm.Controls(strControlName) = varKey
    End Select
    
FormNav_Cleanup:
    
    Set frm = Nothing
    
    
End Sub

Any insight would be greatly appreciated.

Access 2010
 
I think I yield... Playing with the code has rendered it consistent in not clicking buttons for each case. So it was a fluke that it worked rather than not from where I am sitting.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top