Results 1 to 15 of 15
  1. #1
    ScubaBart is offline Competent Performer
    Windows 7 32bit Access 2007
    Join Date
    Jul 2024
    Posts
    117

    Trying to use Public variables but syntax is kicking my butt.

    Still working with the Apiary / Hive / Bees app. Making progress and learning stuff about coding but trying to implement a couple Public Variables to make navigation and such easier but I simple cannot get the syntax to work for me or I am using them wrong, not sure. I have attached the current version of the file and using screenshots, am able to show the problems I am running into.

    To start, begin by opening the F_Main_Overview form.

    Once open, click the Add/Edit Inspections button
    Click image for larger version. 

Name:	Add_Inspect.PNG 
Views:	23 
Size:	10.7 KB 
ID:	52356
    Then apply the Apiary Filter for "Apiary Test # 5" (errors are for any Apiary but this makes it easier to use the exact same sequence.)
    Click image for larger version. 

Name:	Filter_Apiary.PNG 
Views:	23 
Size:	12.4 KB 
ID:	52357


    Select the Open Apiary Button for the first Inspection entry.
    Click image for larger version. 

Name:	Open_Apiary_Insp.PNG 
Views:	23 
Size:	12.8 KB 
ID:	52358
    Click the Add Hive Inspection button. This is where the first code error pops up.
    Click image for larger version. 

Name:	Add_Hive_Insp.PNG 
Views:	23 
Size:	11.6 KB 
ID:	52359
    The form opens but while building the app, I put in temporary fields to make sure the Public variables were being brought into the new form and they are not.

    The error is where I am trying to apply the value of the Public Variable(s) into the temporary fields. I've tried every type of syntax I can fathom but nothing is working. With quotes, without quotes, using the Me.fieldname format and combinations of that. The same error shows for either of the variables if I comment one out temprarily.

    The second error is when I try to close the form, I'm presently using the Cancel option but the same error is shown if I use the Save & Close option but I'm not doing that as it would keep adding more entries to deal with.

    Am I using the Public Variables wrong ? They are declared at the beginning of the VBA_Module code. I even tried to declare the PrevForm variable as "Public PrevForm As Form" which required using the Set function when assigning a value to the variable but I still couldn't get anywhere.


    Any help with this is appreciated. I've searched online and the syntax I have presently is based on actual examples I found in a couple other forums. I couldn't show any more graphics as the post limits me to five uploaded files/photos.
    Attached Files Attached Files

  2. #2
    xps35's Avatar
    xps35 is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Jun 2022
    Location
    Schiedam, NL
    Posts
    299
    "Me" is used to refer to controls on the current form. There is no control named Test_1 on the form.
    Due to the complexity of your application it is hard to give a solution.
    Groeten,

    Peter

  3. #3
    xps35's Avatar
    xps35 is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Jun 2022
    Location
    Schiedam, NL
    Posts
    299
    Edit: removing the "me!" solves the error, but I am not sure that is The Solution.
    Groeten,

    Peter

  4. #4
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,556
    Error is on
    Me!Test_1 = PrevForm

    It is saying no such field as Test_1 ?

    You are looking at the form with the button to open the form with that control on it.
    So set that control the same way you are setting all those other controls?

    Code:
    Private Sub Command_Add_Hive_Insp_Click()  'Adds a new Hive Inspection to the Apiary that is currently shown
    
    
        PrevForm = Me.Name
        RefRecordID = Me.Log_Inspection_ID
        Forms!F_Log_Inspection_Apiary_Detail.Visible = False
        DoCmd.OpenForm "F_Log_Inspection_Hive_Entry_Edit", acNormal, "", "", acFormAdd, , PrevForm
        Forms!F_Log_Inspection_Hive_Entry_Edit!Link_to_Log_Inspection_ID_Isd.Value = RefRecordID
        Forms!F_Log_Inspection_Hive_Entry_Edit!BP_Link_to_Inspection_ID.Value = Forms!F_Log_Inspection_Hive_Entry_Edit!Log_Inspection_Detail_ID
        Forms!F_Log_Inspection_Hive_Entry_Edit!BS_Link_to_Inspection_ID.Value = Forms!F_Log_Inspection_Hive_Entry_Edit!Log_Inspection_Detail_ID
        Forms!F_Log_Inspection_Hive_Entry_Edit!HP_Link_to_Inspection_ID.Value = Forms!F_Log_Inspection_Hive_Entry_Edit!Log_Inspection_Detail_ID
        Forms!F_Log_Inspection_Hive_Entry_Edit!QC_Link_to_Inspection_ID.Value = Forms!F_Log_Inspection_Hive_Entry_Edit!Log_Inspection_Detail_ID
        
        DoCmd.Close acForm, "PrevForm"
        'Me!Test_2.Value = RefRecordID
        'Me.Test_1 = PrevForm
            
    End Sub
    Along the lines of
    Forms!F_Log_Inspection_Hive_Entry_Edit!Test_1 = PrevForm


    Oh and DoCmd.Close acForm, "PrevForm" is going to try and close a form by that name, which does not exist.
    You are using PrevForm as a literal and not a variable.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  5. #5
    madpiet is offline Expert
    Windows 10 Office 365
    Join Date
    Feb 2023
    Posts
    565
    first ten seconds I noticed you have a thing against Explicit variable declaration. Yuck.

    What is PrevForm? A variable? Where is it declared? is PrevForm a global variable? Then where's the global prefix? I'm not totally nuts about Hungarian notation, but the alternatives are worse.

    Also, What are you trying to accomplish? Sounds like you're looking at a technology and searching for a problem to use it on instead of the other way around.

  6. #6
    davegri's Avatar
    davegri is online now Excess Access
    Windows 11 Access 2021
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,741
    The error for Test_1 is because Test_1 does not exist on THAT form.

    For the cancel action:
    Code:
    Private Sub Command_Cancel_Click()  'Removes any changes or entries and returns to the previous form
    
    
        
        If (Forms!F_Log_Inspection_Hive_Entry_Edit.Dirty = -1) Then  'Removes entries to cancel without saving
            
            DoCmd.RunCommand acCmdUndo
            
        End If
        
        If Not IsNull(Me.OpenArgs) Then  'Confirms previous form was documented
            PrevForm = OpenArgs
            DoCmd.Close acForm, "F_Log_Inspection_Hive_Entry_Edit"
            'davegri add below, it actually sets a filter
            DoCmd.OpenForm PrevForm, , , "Log_Inspection_ID =" & RefRecordID
    
    
        Else   'If previous form was not documented then returns to Inspection Main
        
            DoCmd.Close acForm, "F_Log_Inspection_Hive_Entry_Edit"
            DoCmd.OpenForm "F_Log_Inspection_Main"
        
        End If
        
        DoCmd.Requery
    
    
        
    End Sub

  7. #7
    jestinesari is offline Novice
    Windows 11 Office 365
    Join Date
    Oct 2024
    Location
    Delhi
    Posts
    2
    Thanks for sharing! I also have this problem!

  8. #8
    ScubaBart is offline Competent Performer
    Windows 7 32bit Access 2007
    Join Date
    Jul 2024
    Posts
    117
    Wow !! More response than I expected and so many question to as what I am doing so I will try to expand what is going on and how I am trying to do it while answering questions for how and why.

    First, the two Public Variable are declared in the beginning of my VBA_Module code. They are declared as Public, with PrevForm as a String and RefRecordID as a Long Integer.
    The purpose of the PrevForm variable is to assign and call back the true name of a Form(A) when I change to a different Form(X). Since Form(X) can be opened with commands from multiple forms, ie Form(A), Form(B), Form(C), etc, I wanted a simple way to recall the calling form without having to declare the variable explicitly in each form’s code.

    Similarly, I want to use the RefRecordID Variable to assign and call back a specific record identifier, typically the Primary Key value, and since I do this multiple times throughout the application, it seemed easiest to create the Public Variable.
    This approach was based on some input I have received previously and it seemed a clean way to approach the desired goal.

    Second, the form the opens and generates the erors in this series of events is F_Inspection_Hive_Entry_Edit. The screenshot here shows there are two unbound fields in the Header named Test_1 and Test_2 respectively (The labels are generic as these are fields for testing only).

    Click image for larger version. 

Name:	Showing_Test_Fields.PNG 
Views:	15 
Size:	28.6 KB 
ID:	52363

    This form is [supposed] to be the active form when I call on these fields to be populated with the values of the variables so I’m not sure why everyone including the program is saying these fields don’t exist. That is one of the issues I can't figure out.

    To specific points:

    ---From Welshgasman - Oh and DoCmd.Close acForm, "PrevForm" is going to try and close a form by that name, which does not exist.
    You are using PrevForm as a literal and not a variable.---

    I tried different syntax and it appears that using it with the quotes closes the form identified as PrevForm. On my screen that form no longer appears.

    ---From madpiet - What is PrevForm? A variable? Where is it declared? is PrevForm a global variable? Then where's the global prefix? I'm not totally nuts about Hungarian notation, but the alternatives are worse.---

    From my very limited knowledge, declaring a Public Variable in a common VBA module (VBA_Modulein my case) makes it Global ?? If not, then that is something I’m not aware of. I don’t know what a global prefix is. Hungarian notation ??

    ---From davegri – I think I tried that syntax on the cancel action but will try again just in case. ---

    Hopefully this makes everything a bit clearer on what I am trying to do. The alternative I suppose is to simple go back and declare each variable locally in each section of code where needed but it seemed that creating the common Public Variable that was available to all the from modules would be simpler.

    Will get some time later today maybe to try a few of the suggestions and will advise.

  9. #9
    davegri's Avatar
    davegri is online now Excess Access
    Windows 11 Access 2021
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,741
    This form is [supposed] to be the active form when I call on these fields to be populated with the values of the variables so I’m not sure why everyone including the program is saying these fields don’t exist. That is one of the issues I can't figure out.
    The code is trying to refer to a textbox on a different form, not its own form.

    Click image for larger version. 

Name:	NotExist.png 
Views:	15 
Size:	95.7 KB 
ID:	52364

  10. #10
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,556
    You are opening HiveEntryEdit, but that code is running in the previous form.There is no Test_1 control on the previous form, look for yourself.
    If you look at what I suggested, that will do what you want.

    YOU are getting confused as to which form you are in.
    I would be as well with the extra long form names, especially when I do not know the system.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  11. #11
    ScubaBart is offline Competent Performer
    Windows 7 32bit Access 2007
    Join Date
    Jul 2024
    Posts
    117
    Quote Originally Posted by davegri View Post
    The code is trying to refer to a textbox on a different form, not its own form.
    Gotcha. Even though the previous command closed that form, using Me still refers to form connected to that module in particular ? Guess I didn't think about that. Could explain a lot. That is just more of not fully grasping the peculiarities of the syntax.

    Will change to the full syntax for the desired from and hopefully that will address my issues. Will get to try later on.

  12. #12
    ScubaBart is offline Competent Performer
    Windows 7 32bit Access 2007
    Join Date
    Jul 2024
    Posts
    117
    Quote Originally Posted by Welshgasman View Post
    You are opening HiveEntryEdit, but that code is running in the previous form.There is no Test_1 control on the previous form, look for yourself.
    If you look at what I suggested, that will do what you want.

    YOU are getting confused as to which form you are in.
    I would be as well with the extra long form names, especially when I do not know the system.
    Got the same response from davegri. This is more of me not fully grasping the syntax. Will make adjustments and test later when time permits.

  13. #13
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,556
    It is not the syntax, but where the code is running.
    Your syntax looks correct.
    You could also use OpenArgs in the OpenForm command.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  14. #14
    ScubaBart is offline Competent Performer
    Windows 7 32bit Access 2007
    Join Date
    Jul 2024
    Posts
    117
    Quote Originally Posted by Welshgasman View Post
    It is not the syntax, but where the code is running.
    Your syntax looks correct.
    You could also use OpenArgs in the OpenForm command.
    Got it. I am using the OpenArgs to carry the PrevForm variable over but in theory, since it is a Public variable, I shouldn't even have to do that, right ?

  15. #15
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,556
    Correct, you could just use it in the destination form.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 1
    Last Post: 10-16-2019, 03:45 PM
  2. Replies: 11
    Last Post: 04-19-2019, 02:40 PM
  3. Replies: 4
    Last Post: 01-25-2019, 09:03 PM
  4. Using Public variables between forms/queries/reports
    By dcrake in forum Sample Databases
    Replies: 2
    Last Post: 12-25-2015, 05:44 PM
  5. public variables
    By zul in forum Programming
    Replies: 3
    Last Post: 08-23-2011, 11:11 AM

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