Results 1 to 5 of 5
  1. #1
    5 Star Lounger
    Join Date
    Nov 2001
    Location
    Toronto Canada
    Posts
    920
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Public Function (A2K)

    Based on suggestions from a friend, I've started to delve into the wonderful world of Public Functions and it appears to be a nice way of not having to keep repeating identical code in various forms modules.

    However #1, and isn't there always a However, I've run into a couple of hitches.

    Albeit a short snippet of code which would make some wonder why I want to use a Public Function this is my current problem.

    This code is in my form module:

    Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    If KeyCode = vbKeyF9 Then
    Call FKey_F9_GoPrevious
    End If
    End Sub

    When I run it, it bounces me to the Public Module and gives me the message "Invalid use of Me keyword", which after reading the help messges involved, unfortunately makes sense. It does kill the ability of not going past the first record.

    Public Function FKey_F9_GoPrevious()
    If KeyCode = vbKeyF9 Then
    If Me.CurrentRecord > 1 Then
    DoCmd.GoToRecord , , acPrevious
    KeyCode = 0
    End If
    End If
    End Function

    However #2, when I changed the code to the following:

    Public Function FKey_F9_GoPrevious()
    If KeyCode = vbKeyF9 Then
    DoCmd.GoToRecord , , acPrevious
    KeyCode = 0
    End If
    End Function

    Nothing happens in the regular form when F9 is pressed.

    Would appreciate any help with this problem
    Cheers,
    Andy

  2. #2
    5 Star Lounger
    Join Date
    Nov 2004
    Location
    Wilmington, North Carolina, USA
    Posts
    1,196
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Public Function (A2K)

    Try this:

    Public Function FKey_F9_GoPrevious()
    If Me.CurrentRecord.EOF = True
    DoCmd.GoToRecord , , acPrevious
    KeyCode = 0
    End If

    End Function
    ____________________________
    Jeremy
    "If you spend more on coffee than on IT security, then you will be hacked. What's more, you deserve to be hacked." -Richard Clarke

  3. #3
    5 Star Lounger
    Join Date
    Nov 2001
    Location
    Toronto Canada
    Posts
    920
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Public Function (A2K)

    Jeremy, thanks for jumping in here. I used your code, but had to add the then, so that it looked like this:

    Public Function FKey_F9_GoPrevious()
    If Me.CurrentRecord.EOF = True Then
    DoCmd.GoToRecord , , acPrevious
    KeyCode = 0
    End If
    End Function

    And, unfortunately, I still get the "Me" error message when I do the Debug compile. If it worked on yours, maybe there's something not quite right on my version of A2K.

    To add injury to insult, I'm trying to get this to work on both the KeyPress of F9 as well as a Command Button OnClick version of F9. I'm trying to give the user the option of pressing the "real" Function Key or clicking the 'clickable pseudo' Function Key.
    Cheers,
    Andy

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Public Function (A2K)

    "Me" can only be used in the module behind a form (or report). In a standard module, Me doesn't refer to anything.

    There are (at least) two ways to handle this:

    1) You can use CodeContextObject instead of Me in the code in the standard module. CodeContextObject refers to the object (form or report) from which the code is called. So you'd use

    If CodeContextObject.CurrentRecord > 1 Then

    Note: CurrentRecord.EOF is not valid. CurrentRecord is a number, not an object with properties.

    2) You can pass the form to the function:

    In the form module:

    Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    If KeyCode = vbKeyF9 Then
    Call FKey_F9_GoPrevious(Me)
    End If
    End Sub

    In the standard module:

    Public Function FKey_F9_GoPrevious(frm As Form)
    If frm.CurrentRecord > 1 Then
    DoCmd.GoToRecord , , acPrevious
    KeyCode = 0
    End If
    End Function

  5. #5
    5 Star Lounger
    Join Date
    Nov 2001
    Location
    Toronto Canada
    Posts
    920
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Public Function (A2K)

    Hans,

    Has anyone ever asked you a logical question that you couldn't answer? The reason I ask is that, once more, you've broadened my Access coding horizons by coming up with the exact solution needed. It mindles my boggle! Thanks again,
    Cheers,
    Andy

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •