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

    Public Function - Part Deux (A2K)

    Once more have I been able to snatch defeat from the jaws of victory.

    The following are 2 entries in my Public Functions Module

    Public Function FKey_F2_Cancel()
    If KeyCode = vbKeyF2 Then
    DoCmd.DoMenuItem acFormBar, acEditMenu, acUndo, , acMenuVer70
    End If
    End Function


    Public Function butFKey_F2_Cancel()
    DoCmd.DoMenuItem acFormBar, acEditMenu, acUndo, , acMenuVer70
    End Function

    The following are my 2 entries in my Form Module:


    Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    If KeyCode = vbKeyF2 Then
    Call FKey_F2_Cancel
    End If
    End Sub

    Private Sub txtF02_Click()
    Call butFKey_F2_Cancel
    End Sub


    When I enter data into a field and CLICK on a button called txtF02, my entry is neatly cancelled.

    When I enter data into a field and PRESS on the Function Key F2, the field gets highlighted and just sits there and looks at me.

    Other combinations of the above scenario provide the same split results, pressing the Function Key does nothing and clicking on the button works.

    I am obviously missing something and would appreciate any help,
    Cheers,
    Andy

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

    Re: Public Function - Part Deux (A2K)

    From your result, I would guess that
    a) You haven't checked "Require variable declaration" in Tools | Options in the Visual Basic Editor, and/or
    [img]/forums/images/smilies/cool.gif[/img] You don't have Option Explicit at the top of your module.
    Otherwise, you'd have got an error that KeyCode wasn't recognized in FKey_F2_Cancel.
    I very strongly recommend that you turn on Require Variable Declaration (cf <post#=443,622>post 443,622</post: > !). This will insert Option Explicit at the top of each new module you create. You'll have to add it manually in existing modules. See <post#=314748>post 314748</post: > for an explanation why this is useful.

    You don't have to check KeyCode in FKey_F2_Cancel, since you already did that in Form_KeyDown. And you can replace the antiquated DoMenuItem code with RunCommand acCmdUndo:

    Public Function FKey_F2_Cancel()
    RunCommand acCmdUndo
    End Function

  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 - Part Deux (A2K)

    Hans,

    I'm going to break this reply / question into parts in order to facilitate your hopefully helping me with this problem. I've also eliminated some of the code in the middle of the Delete portion that doesn't pertain to the problem in order to save space in this reply

    And please appreciate by the time I/we get through this, I will have the expertise needed to cover the majority of future problems with respect to handling Public Functions which is very crucial for me.

    Part A

    I had checked "Require variable declaration" and I did have "Option Explicit" in all of my modules, EXCEPT for my Modules modAGMConFunctions.

    Part B

    F2 Cancel Button Problem

    The following are the lines of code in modAGMConFunctions that pertains to this portion

    Option Compare Database
    Option Explicit

    Public Function FKey_F2_Cancel()
    RunCommand acCmdUndo
    End Function

    Public Function butFKey_F2_Cancel()
    RunCommand acCmdUndo
    End Function

    The following are the lines of code in the form that pertains to this portion

    Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    If KeyCode = vbKeyF2 Then
    Call FKey_F2_Cancel
    End If
    End Sub

    Private Sub txtF02_Click()
    Call butFKey_F2_Cancel
    End Sub


    If I enter data into a blank field:

    1. Pressing F2 clears the field
    2. Clicking on pseudo command button which triggers txtF02 clears the field

    If I add data at the end of an existing field of data:

    1. Pressing F2 clears the data that I added, BUT, it highlights the original data
    2. Clicking on pseudo command button which triggers txtF02 clears the field and leaves the original data untouched.

    Part C

    F3 Delete Key

    The following are the lines of code in modAGMConFunctions that pertains to this portion

    Public Function FKey_F3_Delete()
    If KeyCode = vbKeyF3 Then
    On Error GoTo Err_FKey_F3_Delete_Click

    . . . . . . stuff cut out

    Resume Exit_FKey_F3_Delete_Click
    KeyCode = 0
    End If
    End Function


    The OnClick Version worked so no code is shown


    1. Pressing F3 gave me a "Variable Not Defined" message and highligted If KeyCode, etc.
    2. So I deleted the "If KeyCode = vbKeyF3 Then" line and its' End If
    3. Pressing F3 again gave me a "Variable Not Defined" message, and highligted KeyCode = 0
    4. So I deleted the KeyCode=0 and it deleted the record correctly with all of the correct messages
    5. So the question in this case: I thought that I had to have KeyCode = 0 in this type of coding situation?

    6. Clicking on pseudo command button which triggers txtF03 deletes the record with all of the correct messages


    So basically, until I hear further, if I get rid of the If / End If statements and the Keycode = 0 lines of code, I should be OK. As a matter of fact, to be on the safe side, I'm not going to go any further until this is confirmed.

    Hans, again, thanks very kindly for helping me for this,

    ps. Totally off the subject, but I've seen something on the Forum that explains how I can keep my indents, etc. when posting, but can't find it
    Cheers,
    Andy

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

    Re: Public Function - Part Deux (A2K)

    To tackle your PS first: you can put code between <!t>[pre]<!/t> and <!t>[/pre]<!/t> tags to preserve indentation. Be careful not to include very long lines, they will cause horizontal scrolling, especially on smaller screen resolutions. The downside is that code displayed that way can't be copied directly from a post into the Visual Basic Editor. You have to paste it into a Word document first, then copy it again.

    Another method is to replace indents with <!t>[tab[/b] tags. I copy code from VB into a Word document, then run the macro from <post#=346,967>post 346,967</post: > to create the <!t>[tab]<!/t> tags automatically, then copy the result into a post.

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

    Re: Public Function - Part Deux (A2K)

    Part B

    The difference in behavior is to be expected - when you press F2, the focus is presumably in the text box itself, when you click the command butto, the focus is obviously on the command button.

    Part C

    KeyCode is an argument of the On KeyDown even procedure, but you don't pass it to the public function, so it doesn't know anything about it. Modify as follows:

    Public Function FKey_F3_Delete(KeyCode As Integer)
    If KeyCode = vbKeyF3 Then
    On Error GoTo Err_FKey_F3_Delete_Click

    . . . . . . stuff cut out

    Resume Exit_FKey_F3_Delete_Click
    KeyCode = 0
    End If
    End Function

    And call it like this from the Form_KeyDown event procedure:

    Call FKey_F3_Delete(KeyCode)

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

    Re: Public Function - Part Deux (A2K)

    Hans,

    I promise not to bother you again for at least 12 - 24 hours. Thanks again,
    Cheers,
    Andy

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

    Re: Public Function - Part Deux (A2K)

    Don't worry - asking questions is what the Lounge is for.

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

    Re: Public Function - Part Deux (A2K)

    Hans,

    OK, the 24 hours are up and sure enough the short strokes have taken me down to these last two ( so far ) anomalies.

    Both of these calls give me "Invalid use of Property"

    '== Click ================== Add Button ===========
    Private Sub txtbutAdd_Click()
    Call butAdd
    End Sub

    '== Click ================== Receive Button =======
    Private Sub txtbutReceive_Click()
    Call butReceive
    End Sub


    '== Click ================== Add Button ==========
    Public Function butAdd()
    RunCommand acCmdClose
    DoCmd.OpenForm FormName:="frm Inventory - 01 - AddNew", DataMode:=acFormAdd, OpenArgs:="View"
    End Function

    '== Click ================== Receive Button ======
    Public Function butReceive()
    RunCommand acCmdClose
    DoCmd.OpenForm "frm Inventory - 02 - Received"
    End Function

    Where exactly have I not seen the light this time, and I know for sure it's going to be something obvious. Also, do you know of a good source of a clear explanation of this whole Public/Private Function scenario that I could look at so as to not continually coming to the Forum. I certainly don't mind spending the time reading if I can find a good source.

    As always, any help is appreciated,
    Cheers,
    Andy

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

    Re: Public Function - Part Deux (A2K)

    I don't see anything wrong with the code you post. However, I wonder why you want to move the code to a standard module; the butAdd and butReceive functions seem specific rather than general.

    Public/Private specifies the "scope" of a sub/function/constant/variable.

    If an item is declared as Private, it will be known only in the module containing the declaration. The item will be unknown outside the module. Two different modules can contain Private declarations of items with the same name, there will be no confusion. Because event procedures are specific to the form or report, they are declared as Private by default.

    If an item is declared as Public, it will be known throughout all modules. There can be only one Public declaration of a name, otherwise you'll get an error message.

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

    Re: Public Function - Part Deux (A2K)

    Hans,

    Yes you were correct in that the code that I posted was correct. However, I inadvertantly mislabelled one of the sets of on-form buttons thereby causing the error. The reason that I used the Private/Public function scenario in this case is that first, this is a whole new "toy" for me to play with and also in this case, these sets of buttons are used in different combinations on four different forms, so I thought I'd set the code up as a Public Function.

    Also being a really, old Mainframe programmer using Cobol, we use to use a "Perform" function which would allow us to use a routine any number of times and only have to programme it once. So if you screwed up code, you only screwed up in one place making it easier to clean up. Anyway, that's my story and I'm sticking to it.

    Thanks kindly for explaining the Private / Function scenario in such a clear and lucid fashion.
    Cheers,
    Andy

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

    Re: Public Function - Part Deux (A2K)

    Hans,

    Just to extend the usefullness, in my mind anyway, of Public Functions, I'd like to be able to do the following:


    Public Function butCallForm()
    DoCmd.Close
    DoCmd.OpenForm "X"
    End Function


    Private Sub txtKeyV_Click()
    Call butCallForm - with whatever form it is I want to call at this point replacing the "X"
    End Sub

    It may seem like a small thing, but it would be very useful in my particular situation.

    Thanks in advance as always,
    Cheers,
    Andy

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

    Re: Public Function - Part Deux (A2K)

    As in the keyboard functions, you can pass an argument:

    Public Function butCallForm(strFormName As String)
    DoCmd.Close
    DoCmd.OpenForm strFormName
    End Function

    Private Sub txtKeyV_Click()
    Call butCallForm("frmMyForm")
    End Sub

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

    Re: Public Function - Part Deux (A2K)

    Hans,

    Thanks kindly again. It is the string concept that I will eventually get my head around.
    As always, I hope that my simplistic questions can help others,
    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
  •