none
Moving vba code from form module to general module RRS feed

  • Question

  • Hi folks,  I have two problems.

    My program stores records of clients.

    I am using a switch board with 6 command buttons.  Each button opens a similar but different form.  I use a global variable to remember which client the user is looking at as you move from one form to another.  When the new form is opened it uses the global to identify the client, show the selection in a combo box, and then move to that record.  

    Problem 1: It works for 5 forms, but does not work for the 6th form, even though the code is exactly the same.

    Problem 2:  I want to move the code to a general module, but the code does not know where to look, since it is no longer tied to the form.

    This is the code.  I would like to move it to a module.  But the "SearchForRecord" will not work.

    Private Sub Form_Open(Cancel As Integer)
        cboCaseNumSelector = glblCaseID
        cboCaseCodeSelector = glblCaseCode
        DoCmd.SearchForRecord , "", acFirst, "CaseCode = " & "'" & [cboCaseCodeSelector] & "'"
    End Sub

    Monday, May 2, 2016 5:11 AM

Answers

  • This is the code.  I would like to move it to a module.  But the "SearchForRecord" will not work.

    Private Sub Form_Open(Cancel As Integer)
        cboCaseNumSelector = glblCaseID
        cboCaseCodeSelector = glblCaseCode
        DoCmd.SearchForRecord , "", acFirst, "CaseCode = " & "'" & [cboCaseCodeSelector] & "'"
    End Sub

    Hi Verbalist,

    In the different forms you can call the code in the general module by including the form object as parameter, e.g.

            Private Sub Form_Open(Cancel As Integer)
                My_general_code Me, par_x, par_y
            End Sub

    In the general module you then have:

            Sub My_general_code(cur_form As Form, par_x, par_y, ...)
                ...
            End Sub

    Now you can refer to the right form using cur_form in the same way you would refer to Me in the original form.

    Imb.

    Monday, May 2, 2016 8:12 AM
  • If I move the code to a general module, I am thinking that I would also need to put a reference in front of my "local" controls - so cboCaseCodeSelector becomes cur_form.cboCaseCodeSelector.  Is that right?

    Also, do you have any ideas why the code works for 5 out of 6 forms which use it, but not the 6th?  Is there too little information?

    Hi Verbalist,

    1. Yes, that is right. All controls of the specific form are now adressed by:   cur_form!Any_control.

    But you can also use the form's methods and properties. but you have to specify the cur_form object.

    2. You have to search for the difference between the 5 forms and the 6th one. From my side I cannot see that, unless you give very detailed information about the forms.

    Imb.

    Tuesday, May 3, 2016 7:46 AM

All replies

  • This is the code.  I would like to move it to a module.  But the "SearchForRecord" will not work.

    Private Sub Form_Open(Cancel As Integer)
        cboCaseNumSelector = glblCaseID
        cboCaseCodeSelector = glblCaseCode
        DoCmd.SearchForRecord , "", acFirst, "CaseCode = " & "'" & [cboCaseCodeSelector] & "'"
    End Sub

    Hi Verbalist,

    In the different forms you can call the code in the general module by including the form object as parameter, e.g.

            Private Sub Form_Open(Cancel As Integer)
                My_general_code Me, par_x, par_y
            End Sub

    In the general module you then have:

            Sub My_general_code(cur_form As Form, par_x, par_y, ...)
                ...
            End Sub

    Now you can refer to the right form using cur_form in the same way you would refer to Me in the original form.

    Imb.

    Monday, May 2, 2016 8:12 AM
  • Hi Edward,

    Thanks for the response.

    If I move the code to a general module, I am thinking that I would also need to put a reference in front of my "local" controls - so cboCaseCodeSelector becomes cur_form.cboCaseCodeSelector.  Is that right?

    Also, do you have any ideas why the code works for 5 out of 6 forms which use it, but not the 6th?  Is there too little information?

    MMS.

    Tuesday, May 3, 2016 6:48 AM
  • If I move the code to a general module, I am thinking that I would also need to put a reference in front of my "local" controls - so cboCaseCodeSelector becomes cur_form.cboCaseCodeSelector.  Is that right?

    Also, do you have any ideas why the code works for 5 out of 6 forms which use it, but not the 6th?  Is there too little information?

    Hi Verbalist,

    1. Yes, that is right. All controls of the specific form are now adressed by:   cur_form!Any_control.

    But you can also use the form's methods and properties. but you have to specify the cur_form object.

    2. You have to search for the difference between the 5 forms and the 6th one. From my side I cannot see that, unless you give very detailed information about the forms.

    Imb.

    Tuesday, May 3, 2016 7:46 AM