Page 1 of 3 123 LastLast
Results 1 to 15 of 34
  1. #1
    bubai is offline Competent Performer
    Windows 7 32bit Access 2016
    Join Date
    Nov 2014
    Location
    Dhaka
    Posts
    162

    Module level variable declaration.


    If I declarer a module level variable with a Dim/ Private keyword, do I have to assign it's value at the same time? If I assign the value while declaration, do get to change it's value in different procedures?

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    Why not just try it out? Would take a few seconds?
    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

  3. #3
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    you can assign it anytime.

  4. #4
    orange's Avatar
    orange is online now Moderator
    Windows 10 Access 2016
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    Perhaps you could tell readers What you are trying to accomplish - there may be options for How to achieve.
    Tempvars??

    More info on scope of variable here.

  5. #5
    bubai is offline Competent Performer
    Windows 7 32bit Access 2016
    Join Date
    Nov 2014
    Location
    Dhaka
    Posts
    162
    If I assign the value while declaration, I get the error as in the pic. It occurs at the click event.
    Click image for larger version. 

Name:	er.jpg 
Views:	35 
Size:	132.8 KB 
ID:	43558
    However if I don't assign in the declaration stage it works fine. The code is below.
    Code:
    Option Compare Database
    Option Explicit
    Private yctr As Boolean
    yctr = False
    
    Private Sub Form_BeforeUpdate(Cancel As Integer)
    If Me.NewRecord Then
    If DCount("Payment_ID", "Payment_CNF") < 1 Then
    Me.Payment_ID = 1
    Else
    Me.Payment_ID = DMax("Payment_ID", "Payment_CNF") + 1
    End If
    End If
    
    
    ' This procedure checks to see if the data on the form has
     ' changed. If the data has changed, the procedure prompts the
     ' user to continue with the save operation or to cancel it.
    
    On Error GoTo Err_BeforeUpdate
    ' The Dirty property is True if the record has been changed.
    If Me.Dirty And yctr = False Then
       If MsgBox("Do you want to save?", vbYesNo + vbQuestion, "Save Record") = vbNo Then
          Me.Undo
       End If
     End If
    
    
    Exit_BeforeUpdate: yctr = False
     Exit Sub
    Err_BeforeUpdate:
     MsgBox Err.Number & " " & Err.Description
     Resume Exit_BeforeUpdate
    yctr = False
    End Sub
    
    
    Private Sub PayAssign_Click()
    Dim FKCNFID As Byte, newAmount As Long, dt As Date, methd As String, _
        Sq As String, xCounter As Boolean
    If Not IsNull(Me.Cons_ID) And IsNull(Me.Cons_ID.OldValue) Then
        FKCNFID = Me.CNF_ID
        newAmount = Me.Amount.OldValue - Me.Amount
        dt = Me.Paid_Dt
        methd = Me.Payment_Method.OldValue
        Sq = "insert into Payment_CNF values (" & (DMax("Payment_ID", "[Payment_CNF]") + 1) & " , " _
        & "NULL" & " , " & FKCNFID & " , #" & dt & "# , " & newAmount & " , '" & methd & "' );"
        If newAmount >= 0 Then
        On Error GoTo out
        DoCmd.RunSQL Sq
        yctr = True
        Me.Dirty = False
        End If
    
    
    out: Me.Undo
    Exit Sub
    End If
    End Sub
    Last edited by bubai; 12-08-2020 at 12:18 AM. Reason: Missed to include some of the code.

  6. #6
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,977
    The reason for the error is because the first 'procedure' has no header line e.g. Private Sub SomethingOrOther_BeforeUpdate(Cancel As Integer) so none of the initial code is part of a procedure
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  7. #7
    bubai is offline Competent Performer
    Windows 7 32bit Access 2016
    Join Date
    Nov 2014
    Location
    Dhaka
    Posts
    162
    Quote Originally Posted by isladogs View Post
    The reason for the error is because the first 'procedure' has no header line e.g. Private Sub SomethingOrOther_BeforeUpdate(Cancel As Integer) so none of the initial code is part of a procedure
    I just put my foot in it!
    Actually the header line was there (ie. Private Sub Form_BeforeUpdate(Cancel As Integer)). I missed to include that in this upload (now corrected- please check). However, the problem persists with this header in place.

  8. #8
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    I don't believe you can assign a value to yctr outside of a procedure? I think I have always set a module level variable within a procedure.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  9. #9
    bubai is offline Competent Performer
    Windows 7 32bit Access 2016
    Join Date
    Nov 2014
    Location
    Dhaka
    Posts
    162
    Quote Originally Posted by Micron View Post
    I don't believe you can assign a value to yctr outside of a procedure? I think I have always set a module level variable within a procedure.
    But Microsoft says otherwise as in the link.
    https://docs.microsoft.com/en-us/off...y#module-level

  10. #10
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,977
    Declaring a variable refers to the line Private yptr As Boolean or more usually Dim ypt As Boolean'
    See Declaring variables

    I also always set the value in a procedure.
    In any case, a boolean value is automatically false when first defined so that line isn't needed there. You should remove it.

    The line is also redundant here:
    Code:
    Err_BeforeUpdate:
     MsgBox Err.Number & " " & Err.Description
     Resume Exit_BeforeUpdate
    yctr = False
    End Sub
    The Resume Exit_BeforeUpdate line means the following line is never executed. You set it to false in the Exit_BeforeUpdate section anyway.

    Can I also suggest you use proper error handling in your other procedure
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  11. #11
    bubai is offline Competent Performer
    Windows 7 32bit Access 2016
    Join Date
    Nov 2014
    Location
    Dhaka
    Posts
    162
    Quote Originally Posted by isladogs View Post
    Declaring a variable refers to the line Private yptr As Boolean or more usually Dim ypt As Boolean'
    See Declaring variables
    This link also says they can be declared outside procedure.

    I also always set the value in a procedure.
    Is there any way to set the value outside the procedure?

    In any case, a boolean value is automatically false when first defined so that line isn't needed there.
    Error also occurs if I set value to some other types of variable (eg integer). I may sometimes need to declare variable other than boolean.

    The line is also redundant here:
    Code:
    Err_BeforeUpdate:
     MsgBox Err.Number & " " & Err.Description
     Resume Exit_BeforeUpdate
    yctr = False
    End Sub
    The Resume Exit_BeforeUpdate line means the following line is never executed. You set it to false in the Exit_BeforeUpdate section anyway.

    Can I also suggest you use proper error handling in your other procedure
    Exit_BeforeUpdate is derived from Err_BeforeUpdate: statement. So if I want to set yctr to False even when there is no error do I put it on the code like this?
    Code:
    yctr = False
    Exit_BeforeUpdate: yctr = False
     Exit Sub
    Err_BeforeUpdate:
     MsgBox Err.Number & " " & Err.Description
     Resume Exit_BeforeUpdate
    End Sub
    Also, for the Sub PayAssign_Click() if I click NO to the append warnings, Run-time error 2501 occurs and it says, "The RunSql action was cancelled." So I put Me.Undo on catch of the error. What else do you suggest?

  12. #12
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,977
    I think I need to clarify what I was saying:

    Quote Originally Posted by bubai View Post
    This link also says they can be declared outside procedure.
    Yes of course they can. I wasn't saying otherwise
    If declared using Dim/Private at module level, their scope applies to the entire module.
    If declared in a procedure, the scope applies to that procedure only.

    Quote Originally Posted by bubai View Post
    Is there any way to set the value outside the procedure?
    Yes e.g. you can set the value in a table but that's not relevant in your situation
    Set the value in the procedure .... but unnecessary in the case of a boolean as it is false by default

    Quote Originally Posted by bubai View Post
    Error also occurs if I set value to some other types of variable (eg integer). I may sometimes need to declare variable other than boolean.
    Just set the value in the procedure

    Quote Originally Posted by bubai View Post
    Exit_BeforeUpdate is derived from Err_BeforeUpdate: statement. So if I want to set yctr to False even when there is no error do I put it on the code like this?
    Code:
    yctr = False  'OMIT THIS LINE IS SUPERFLUOUS
    Exit_BeforeUpdate: yctr = False
     Exit Sub
    Err_BeforeUpdate:
     MsgBox Err.Number & " " & Err.Description
     Resume Exit_BeforeUpdate
    End Sub
    Where there is no error the code automatically moves to the Exit_BeforeUpdate section where you are setting yptr to False. Where an error occurs, the Resume Exit_BeforeUpdate line also ensures that yptr is set to False.
    So there is no need to set it again anywhere else

    Quote Originally Posted by bubai View Post
    Also, for the Sub PayAssign_Click() if I click NO to the append warnings, Run-time error 2501 occurs and it says, "The RunSql action was cancelled." So I put Me.Undo on catch of the error. What else do you suggest?
    What I meant was use proper error handling code like you did in the BeforeUpdate event. I would use something like this:

    Code:
    Private Sub PayAssign_Click()
    
    On Error GoTo Err_Handler
    ....your code here
    
    Exit_Handler:
      Exit Sub
    
    Err_Handler:
      If err=2501 Then Cancel=True
      MsgBox "Error " & err.Number & " : " & err.Description & " in PayAssign_Click procedure, vbCriical, "Program Error"
      Resume Exit_Handler
    
    End Sub
    Hope that helps rather than adds further confusion
    Last edited by isladogs; 12-08-2020 at 08:43 AM. Reason: Restored missing section at start
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  13. #13
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    Declaring a variable is one thing. Assigning a value to it is another. I saw nothing in that link that says you can assign outside of a procedure.
    I'm saying you cannot.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  14. #14
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    I'm with Micron - it tells you as much if you try and compile it;


    Click image for larger version. 

Name:	Screenshot 2020-12-08 154007.png 
Views:	26 
Size:	18.6 KB 
ID:	43567

    And that link does not mention setting variables values outside a procedure only declaring them.
    You can set a constant outside of the procedure.
    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 ↓↓

  15. #15
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,977
    I totally agree that you cannot set variable values in a form/report module outside a procedure

    However, I do routinely set global variable values in a table e.g.

    Click image for larger version. 

Name:	Capture.PNG 
Views:	24 
Size:	50.2 KB 
ID:	43568

    NOTE: all values above are for a fictitious school!

    I then reference those where needed, usually using a user defined function
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

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

Similar Threads

  1. Variable Declaration in MS Access 2013 VBA
    By msaccessdev in forum Programming
    Replies: 7
    Last Post: 06-09-2014, 11:09 AM
  2. Getting Variable from Module to Report
    By dylcon in forum Modules
    Replies: 3
    Last Post: 02-14-2014, 03:43 PM
  3. Create new form instance at the module level
    By DevSteve in forum Modules
    Replies: 1
    Last Post: 09-11-2012, 11:47 AM
  4. Replies: 4
    Last Post: 05-16-2011, 04:58 PM
  5. Variable Table Names in a Module
    By Jessica in forum Modules
    Replies: 1
    Last Post: 04-19-2010, 07:38 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