Page 1 of 2 12 LastLast
Results 1 to 15 of 19
  1. #1
    caubetiep1337 is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Dec 2015
    Posts
    22

    Passing strings between event procedures on form?

    Hey guys,



    I am just freshly starting out with Access and VBA coding. I am using a Form and trying to pass a variable from one event sub to another. But it keeps giving me errors: "Procedure declaration does not match description of event or procedure having the same name".

    A snippet of my code is:

    Code:
     Option Compare Database
    Public Sub Form_Load()
    Dim Fsobj As New FileSystemObject
    Sub RosterChk_Click(Fsobj)
    If FSobj.FileExists("C:\Users\U460809\Desktop\Import\roster.csv") = False Then
    MsgBox "File Does not exists. Please download the extract."
    RosterChk = False
    End If
    Any idea how to get around this? And if it's not possible, can someone explain why? Cause the variable can be referred across subs in a regular Module, right? Thanks!

    -Dat

  2. #2
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,870
    What exactly, in plain English, are you trying to do?

  3. #3
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 8 Access 2013
    Join Date
    Aug 2013
    Posts
    7,862
    Aside from the fact an explanation of what it is you trying to accomplish would be helpful, you have some syntax issues.

    Public Sub Form_Load()
    Dim Fsobj As New FileSystemObject
    Sub RosterChk_Click(Fsobj)

    You merged to procedures together. So you have the beginning of two subs merged together and no End Sub lines, termination lines.

  4. #4
    caubetiep1337 is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Dec 2015
    Posts
    22
    Oh my bad, that was a mistake when copying over parts of the code. In short, I have a Form with checkboxes and a button. Based on what boxes are checked, some data will be imported after the button is clicked. What I want, though, is that upon clicking the checkbox, I want to check whether the file exists or not.

    Code:
    Public Sub Form_Load()
    Dim Fsobj As New FileSystemObject
    Dim Roster, FileLoc as string
    FileLoc = "C:\Users\U460809\Desktop\Import\"
    Roster = "Roster.csv"
    End Sub
    
    Private Sub UpdateBtn_Click()
    'import data, saved in different module
    Call Import_Roster
    End Sub
    
    Private Sub RosterChk_Click(Fsobj, FileLoc, Roster)
    'load the strings here
    If FSobj.FileExists(FileLoc & Roster)= False Then
    MsgBox "File Does not exist. Please download the extract."
    RosterChk = False
    End If

  5. #5
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 8 Access 2013
    Join Date
    Aug 2013
    Posts
    7,862
    So, what is happening is that you are declaring and instantiating the FileSystemObject in the On Load event. When this OnLoad event completes, at the End Sub line, the object is no longer in scope and is not anything, any longer.

    The same goes for the string variables you declared. After the End Sub line, your computer has no reference to them.

    One thing you can do is make declarations in the Module's Header. This way, the variables are available everywhere within the Module. One thing to make note of, you cannot use the New keyword when declaring a complex data type in the Module's Header. In other words, you cannot instantiate the Object there. You are not able to instantiate objects or make assignments to variables in the header.

    So you would need to do something like ...
    Code:
    Option Compare Database
    
    Dim FSobj As FileSystemObject
    Dim Roster, FileLoc As String
    
    Private Sub Command0_Click()
    
    'load the strings here
    If FSobj.FileExists(FileLoc & Roster) = False Then
        MsgBox "File Does not exist. Please download the extract."
    '    RosterChk = False
    End If
    
    End Sub
    
    Private Sub Form_Load()
    
    Set FSobj = CreateObject("Scripting.FileSystemObject")  'instantiate here or in another procedure
    FileLoc = "C:\Users\U460809\Desktop\Import\"    'assign values to variables here or in another procedure
    Roster = "Roster.csv"
    
    End Sub
    Also, I am not sure what your intentions are with RosterChk = False (so I commented that out). Seems you should have another variable declared as type Boolean.

  6. #6
    caubetiep1337 is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Dec 2015
    Posts
    22
    Thank you! Is there a way for me to reference those variables like Roster, As_Is, As_was in different modules, too? Outside the Form module?

  7. #7
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 8 Access 2013
    Join Date
    Aug 2013
    Posts
    7,862
    You can declare variables in a way that they are available on the Public side. However, simple data types, like String, Long, Int, etc. will always be Private when decalred in a Form or Report's module. The simplest is to place them in a Standard Module and declare them as Public.

    Doing the following in a Form's Module will be Private to that module.
    Public strValue As String
    or
    Dim strValue As String

    And, as you learned earlier, the scope, or life, of that variable depends on where, within the module, you place the declaration. The take away here is the above declaration is Private and not available on the Public side.

    If you want a variable to be avaialbe on the Public side, you need to place it in a Standard Module or a Custom Class. A simple explanation/example is to place the following in the header area of a Standard module. In other words, not in a function or other procedure, like a Sub-Procedure.
    Public strValue As String

    Declaring your variable as Public and in a Standard Module causes the variable to be available on the Public side. In other words, available to any other Module. There are rules and exceptions regarding some objects gaining access to Public variables, like tables and queries.

    One last thing to remember about using variables that are simple data types, they are volatile. In other words, if there is an error/exception, the reference will be erased.
    Last edited by ItsMe; 12-11-2015 at 04:28 PM. Reason: Technical error.

  8. #8
    caubetiep1337 is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Dec 2015
    Posts
    22
    But what about assigning a value to the variable that will be saved and used across modules? Something like this?
    Code:
     
    'Form Module
    Option Compare Database
    Public StrName As String
    StrName = "test"
    
    Sub blabla()
    '...
    End Sub
    And if I go to another module and write let's say
    Code:
     
    Module1
    
    
    MsgBox StrName
    will it say "test" or will it be blank?

    Thanks!

  9. #9
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 8 Access 2013
    Join Date
    Aug 2013
    Posts
    7,862
    I am going to have to edit my other post. I made a mistake. You cannot access simple data types on the Public side when they are declared in a form's module. You can access a procedure, but not a String Variable.

    So, what you will have to do is use a Standard Module for your ...
    Public StrName As String

    And the other thing you need to pay attention to is where you do your assignment.
    StrName = "test"

    The assignment needs to be done in a procedure. So you could do it in a Form's load event or a click event or a function, etc.

  10. #10
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    And one more thing. This statement
    Code:
    Dim Roster, FileLoc As String
    declares "Roster" as a variant type and FileLoc As String.

    In VBa, to have both declared as strings, you must explicitly declare the variables
    Code:
    Dim Roster As String, FileLoc As String

  11. #11
    caubetiep1337 is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Dec 2015
    Posts
    22
    @ssanfu

    thanks!I initially learned VB with Visual Studio so it's good to know there are syntax differences!

    @ItsMe

    thanks for clarifying and explaining the concepts!

    Cheers!

  12. #12
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 8 Access 2013
    Join Date
    Aug 2013
    Posts
    7,862
    Also, you do not have to use a Standard Module. You can use the existing Classes associated with forms and also create your own classes. Like I mentioned, you can create procedures in a Form's module and make these available on the public side.

    For instance, you can make a click event Public and call the click event from another form. Also, you can create constructers. Here is an example of a procedure to create a property field of a form's class module.
    Code:
    Public Property Get MyPractice() As String
        MyPractice = p_strPractive
    End Property
    So you could declare p_strPractive in the Module's header ...
    Code:
    Dim p_strPractive as string
    And then assign the value to the private variable in the same form's load event or a click event.
    Code:
    p_strPractive = "Hello World"
    Then, from another form, you can use the full namespace of the MyPractice field from another form.
    Code:
     MsgBox Forms!FirstFormName.MyPractice

  13. #13
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    Hi -

    Referring back to your first post, about the error message.

    If this line:

    Sub RosterChk_Click(Fsobj)

    is the first line of the On_click event procedure of a button called RosterChk, that is what is causing the error. The On Click event procedure does not have any parameters, and you will get that error if you try to use any.

  14. #14
    caubetiep1337 is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Dec 2015
    Posts
    22
    Thanks guys, I made it work so that I can call in variables to the click procedure! I do have another thing. I want to write a code that's based on whether a checkbox is checked or not, but in a standard module (not in the form module) so that in the actual form module, I can just call the sub in the button event. E.g.

    Code:
     
    Module1
    Sub Example()
    If Chkbox = True Then
    ...
    End if
    End Sub
    In the Form Module
    Code:
    Form1
    Private Sub Btn_Click()
    Call Example
    End Sub
    So far, in the standard module, it doesn't read whether checkbox has been checked or not. I want to AVOID going into the Form module and write something like this:

    Code:
    Form1
    Private Sub Btn_Click()
    If Chkbox = True Then
    Call Example
    End if
    End Sub
    Cause I have multiple things that I want to activate with the button and just want to have something like:
    Code:
    Form1
    Private Sub Btn_Click()
    Call Example
    Call Example2
    Call Example3
    ...
    
    End Sub

  15. #15
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    It's easily done. All you need to do is pass the reference to the form as an argument to the sub.

    So, in your standard module1 you would have:

    Code:
    Sub Example(frm as Form)
    If frm.Chkbox = True Then
    ...
    End if
    End Sub
    and in the form code, you would have:

    Code:
    Private Sub Btn_Click()
    Call Example Me
    End Sub
    or

    Code:
    Private Sub Btn_Click()
    Call Example Me
    Call Example2 Me
    Call Example3 Me
    ...
    
    End Sub

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 5
    Last Post: 11-17-2014, 06:18 PM
  2. Replies: 3
    Last Post: 02-11-2012, 11:17 AM
  3. Event Procedures
    By jlgray0127 in forum Forms
    Replies: 6
    Last Post: 11-04-2011, 02:24 PM
  4. Event procedures
    By GIS_Guy in forum Forms
    Replies: 1
    Last Post: 05-11-2010, 02:34 PM
  5. Replies: 0
    Last Post: 01-08-2009, 05:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums