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

    Learning Dim's and how to use them

    Still on my Bee Apiary / Hive project. Been a few delays but working as time permits.



    Getting the gist of using VBA is getting easier but still run into a brick wall here and there.

    So, Issue here is, I am using the Dim function to set the value of the form that opens another form and then using the variable as an OpenArgs value so when I close the 2nd form, it will return me to th eoriginal calling form as it could be one of 2 or 3.

    As I have multiple ways to exit the 2nd form (saving and exiting or canceling and exiting) I am trying to figure out if I need to establish the Dim variable in each Private Sub or if I can declare it once in an OnOpen sub for the 2nd form and call on it in any of the other Private Subs that need to return to the previous form.

    If this doesn't make sense, I can post the code.

    Thanks.....

  2. #2
    davegri's Avatar
    davegri is online now Excess Access
    Windows 11 Access 2019
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,741
    try a public variable in a code module (not a form module, because it should be available for all the forms).

    Option Compare Database
    Option Explicit
    Public CallingForm As String



    When any form calls another form, have it put its name in the public variable before the DoCmd.OpenForm.

    Private Sub MyFormBtn_Click()
    CallingForm = Me.Name
    DoCmd.OpenForm "MyForm"
    DoCmd.Close acForm, Me.Name
    End Sub



    The called form:
    Private Sub Form_Close()
    If Len(CallingForm) > 0 Then
    DoCmd.OpenForm CallingForm
    CallingForm = ""
    End If
    End Sub



    No matter how the called form closes, it will execute the form_close event. It can check there to see who to return to, and clear the public var.
    Last edited by davegri; 10-15-2024 at 10:53 PM. Reason: added examples

  3. #3
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    This is all about scope. Bookmark this page as you're likely to need it time and again
    https://learn.microsoft.com/en-us/of...and-visibility

    If that's too confusing, try finding an explanation that works for you.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  4. #4
    ScubaBart is offline Competent Performer
    Windows 7 32bit Access 2007
    Join Date
    Jul 2024
    Posts
    117
    Okay. I get the gist of what you guys are saying but me putting that in action is still outside my skill set. I have a very rudimentary understanding of classes and objects and such but not enough to wrap my head around it and since this is likely the most complex work I will ever do again, if I even do anything at all. I need a little more lower level help.

    To that end, I have posted one of the particular form modules below. In the first command section and the last command section, I use the Dim function to define the PrevForm (stCallingForm as many seem to use) and then since I can exit this form in multiple conditions, one saves and exits and the other cancels and exits, I currently define the Dim variable twice, once for each command.

    What davegri says makes sense I just don't know how to execute that or how the syntax should be put in place. That's where I need some help here. If someone can show me with this code, that would be great.

    Code:
    Option Compare Database
    
    Private Sub Command_Save_Exit_Click()  'Closes Hive Inspection Entry / Edit and returns to previous form
    
    
    Dim PrevForm As String
    
    
        If Not IsNull(Me.OpenArgs) Then  'Confirms previous form was documented
            
            PrevForm = Me.OpenArgs
            DoCmd.Close acForm, "F_Log_Inspection_Hive_Entry_Edit"
            DoCmd.OpenForm PrevForm
            
        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
    
    
    Private Sub Command_Save_Add_Click()
    
    
        If Me.Dirty Then
            Me.Dirty = False
        End If
        
        DoCmd.GoToRecord , , acNewRec
    
    
    End Sub
    
    
    Private Sub Command_Cancel_Click()  'Removes any changes or entries and returns to the previous form
    
    
    Dim PrevForm As String
    
    
        If (Forms!F_Log_Apiary_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 = Me.OpenArgs
            DoCmd.Close acForm, "F_Log_Inspection_Hive_Entry_Edit"
            DoCmd.OpenForm PrevForm
            
        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
    This is not fully tested code so there may be other errors I have to deal with still but defining the variable is what I need the help with at the moment.

    Thanks

  5. #5
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,157
    I think you are making life very complicated for yourself.
    Access is pretty idiot proof out of the box.

    If you are on form 1 and open form 2 without closing form 1, when you close form 2 form 1 will be the active object automatically.
    No need to record where it was opened.

    If you need to open a pop-up form do some detail editing and refresh the underlying calling form you can control that by opening the pop up in Dialog mode.
    When you do that the code in the calling form stops executing and then continues once the Dialog form is closed, so you do something like this in form 1

    Code:
    ''' Open the pop up form
     DoCmd.OpenForm "frmPopUpDetailEdit", , , , , acDialog
    
    ''' This code won't run until the form above is closed
    ''' Now requery the form I'm in.
     Me.Recordset.Requery
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  6. #6
    ScubaBart is offline Competent Performer
    Windows 7 32bit Access 2007
    Join Date
    Jul 2024
    Posts
    117
    Quote Originally Posted by Minty View Post
    I think you are making life very complicated for yourself.
    Access is pretty idiot proof out of the box.

    If you are on form 1 and open form 2 without closing form 1, when you close form 2 form 1 will be the active object automatically.
    No need to record where it was opened.
    I get what you are saying here and I do exactly that in some situations. Here however, the forms go 3 and 4 layers deep and I don't want a crowded mess of forms on the screen.

    Whether I close the calling form or control its visibility, I still need a reference to call back to that specific form.

  7. #7
    davegri's Avatar
    davegri is online now Excess Access
    Windows 11 Access 2019
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,741
    I currently define the Dim variable twice, once for each command.
    Within the context of your code, this is fine. PrevForm is only known to the procedure (sub) in which it is defined. One has absolutely nothing to do with the other, in spite of their duplicate names, as each is declared in a different sub.

  8. #8
    madpiet is offline Expert
    Windows 10 Office 365
    Join Date
    Feb 2023
    Posts
    566
    Dim keyword just defines a variable. Period. what's hard about that?

  9. #9
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,879
    You should have Option Explicit declared in every module.

    Code:
    Option Compare Database
    Option Explicit
    Too late to test ATM, but I wonder if that sub would work with just

    Code:
    Private Sub Command_Save_Exit_Click() 'Closes Hive Inspection Entry / Edit and returns to previous form
    
    DoCmd.OpenForm nz(Me.OpenArgs,"F_Log_Inspection_Main")
    
    DoCmd.Close acForm, Me.name
    
    End Sub
    I assume there's no need to use a where clause.
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  10. #10
    ScubaBart is offline Competent Performer
    Windows 7 32bit Access 2007
    Join Date
    Jul 2024
    Posts
    117
    Quote Originally Posted by madpiet View Post
    Dim keyword just defines a variable. Period. what's hard about that?
    I have learned that. Learning where, when and how to use it when you are not familiar with any coding takes time and input from those that do know. So I ask questions. Why post a comment that has no learning value ?

    If you want to be of assistance, tell me if the use of the Dim function in my code above is the most efficient way of applying it or would there be a benefit in creating it as a global, public or whatever the proper term is and where I would do that.

    I think davegri posted abut that above and I like the idea of that but I don't know the procedure for creating a 'code module' (naming it, the exact syntax, etc.)

  11. #11
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,879
    Dim is a statement, not a function.

    If you do not include Option Explicit in your module you may, or may not, receive an error without dimming variables. It forces you to declare your variables.
    You should check off "require Variable declarations" in the VBE options menu.
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  12. #12
    davegri's Avatar
    davegri is online now Excess Access
    Windows 11 Access 2019
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,741
    I think davegri posted abut that above and I like the idea of that but I don't know the procedure for creating a 'code module' (naming it, the exact syntax, etc.)
    You already have a code module. Here is how to add the public variable. If you declare a public variable, DON'T declare it anywhere else because that declaration will override the public one.
    Click image for larger version. 

Name:	public.png 
Views:	20 
Size:	37.1 KB 
ID:	52317

  13. #13
    ScubaBart is offline Competent Performer
    Windows 7 32bit Access 2007
    Join Date
    Jul 2024
    Posts
    117
    Quote Originally Posted by davegri View Post
    You already have a code module. Here is how to add the public variable. If you declare a public variable, DON'T declare it anywhere else because that declaration will override the public one.

    Thankyou. That's what I was needing. I am learning though it may be slow at times. I only get an hour here, an hour there to work on this so it takes longer for some methods to sink in.

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

Similar Threads

  1. Learning Access and Database design
    By RogerThat in forum Access
    Replies: 1
    Last Post: 10-02-2016, 05:23 AM
  2. Changing private sub dim to public sub dim
    By Stephenson in forum Programming
    Replies: 14
    Last Post: 10-05-2015, 02:59 PM
  3. Replies: 2
    Last Post: 09-11-2014, 08:50 AM
  4. Learning Access 2003 resources and questions
    By bulldog10 in forum Access
    Replies: 3
    Last Post: 04-06-2011, 02:38 AM
  5. Why can't I use Dim dbs as database
    By Swarland in forum Programming
    Replies: 5
    Last Post: 11-28-2010, 12:27 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