Page 1 of 2 12 LastLast
Results 1 to 15 of 19
  1. #1
    kdleaver1974 is offline Novice
    Windows 10 Access 2013 64bit
    Join Date
    Dec 2024
    Posts
    13

    Why is variable remains "null for no reason even after I set the value to equal something

    OK,

    I have a STRING Variable that will not accept a value. When I "debug" the program, and I look to see if the variable has the correct value, it shows the value as null. What is going on???

    The variable is dimensioned using GLOBAL so the value is retained and used in multiple modules. What am I overlooking?

    Here is the relevant code:




    ElseIf comboAddInvoice_Vendor.ListIndex = 6 Then
    AddInvoiceVendor = "WALTHERS" ' <------------- THIS VARIABLE IS UPDATED
    FLAGShippingCost = "N" ' <------------ THIS VARIABLE REMAINS NULL

    Thanks all.

    Ken L

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 11 Access 2021
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    A variable declared as STRING type cannot be Null. It could be empty string.

    Variable is declared in a general module?

    When do you check the variable content - after the line has executed?

    Are you sure about the spelling?

    Do you have Option Explicit in every module header?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    kdleaver1974 is offline Novice
    Windows 10 Access 2013 64bit
    Join Date
    Dec 2024
    Posts
    13
    Quote Originally Posted by June7 View Post
    A variable declared as STRING type cannot be Null. It could be empty string.

    Variable is declared in a general module?

    When do you check the variable content - after the line has executed?

    Are you sure about the spelling?

    Do you have Option Explicit in every module header?
    Yes, Global is in a General Module labeled "DIMVARIABLES" and is the first thing that is loaded.
    I inserted a breakpoint on the first line after the "End if" statements, see here the entire If-Then block:


    ' ***********************************


    If comboAddInvoice_Vendor.ListIndex = 0 Then

    AddInvoiceVendor = "HEARTLAND"
    FLAGShippingCost = "N"

    ElseIf comboAddInvoice_Vendor.ListIndex = 1 Then

    AddInvoiceVendor = "HOBBYTYME"
    FLAGShippingCost = "Y"

    ElseIf comboAddInvoice_Vendor.ListIndex = 2 Then

    AddInvoiceVendor = "INTERMOUNTAIN"
    FLAGShippingCost = "N"

    ElseIf comboAddInvoice_Vendor.ListIndex = 3 Then

    AddInvoiceVendor = "JOANN"
    FLAGShippingCost = "N"

    ElseIf comboAddInvoice_Vendor.ListIndex = 4 Then

    AddInvoiceVendor = "BOBSMITHIND"
    FLAGShippingCost = "N"

    ElseIf comboAddInvoice_Vendor.ListIndex = 5 Then

    AddInvoiceVendor = "SDCPRODUCTIONS"
    FLAGShippingCost = "N"

    ElseIf comboAddInvoice_Vendor.ListIndex = 6 Then

    AddInvoiceVendor = "WALTHERS"
    FLAGShippingCost = "N"

    ElseIf comboAddInvoice_Vendor.ListIndex = 7 Then

    AddInvoiceVendor = "MICHEALS"
    FLAGShippingCost = "N"

    End If

    ' ***********************************


    If FLAGShippingCost = "N" Then ' <-----THIS IS WHERE I HAVE BREAKPOINT
    Spelling is correct, as the variable is "formated" by what it is by the GLOBAL statement. So if I type GLOBAL FLAGShippingCOST as String, the "OST" is capitalized for all these variables.

    What does Option Explicit do? I have never heard of this. On the top of this and every other module is "Option Compare Database". Should it be something else. Thanks

    Ken L

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 11 Access 2021
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Option Explicit forces variable declaration. It can help locate misspelling when variable is used.

    This is an application setting from the VBE Editor > Tools > Options > check Require Variable Declaration

    This will cause the line to be included with a new module. Manually add to existing modules if you want it.

    If you want to provide db for analysis, follow instructions at bottom of my post.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    kdleaver1974 is offline Novice
    Windows 10 Access 2013 64bit
    Join Date
    Dec 2024
    Posts
    13
    Thanks for the offer, but there are several tables and forms making the database very complicated, and I don't have a "clean" program. I have had professional programmers look at my coding and they roll their eyes at me.

    I made the change to Option Explicit, and it didn't work.

    I did change the name of the variable, and for some reason that works. So could it be the name I am using for this variable? What could be causing this?

    Thanks again.

    Ken L

    FOLLOW UP - I used Debug.print and the variable value prints as being "N" but when I use an IF-THEN statement, so IF FLAGShippingCost = "N" then, it determines it does not equal "N", and during a "pause" I move the cursor over the variable it shows it as empty. This is even more confusing. UGH!

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 11 Access 2021
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    I can't see anything in posted code to cause that. I would have to examine db.

    So why are you using If Then structure to pull Vendor and FLAG data? Isn't that info in Vendors table? Isn't vendor name a column of combobox? Reference combobox columns by Index.

    AddInvoiceVendor = comboAddInvoice_Vendor.Column(1)
    FLAGShippingCost = comboAddInvoice_Vendor.Column(2)

    Probably won't resolve the issue of FLAGShippingCost showing empty for the Subsequent If test but certainly much shorter code.

    Be aware that Global variables lose value when code encounters an unhandled run-time error. This can be a bit annoying when debugging.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  7. #7
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,550
    Be aware that Global variables lose value when code encounters an unhandled run-time error. This can be a bit annoying when debugging.
    That is where Tempvars come in handy.
    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

  8. #8
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,550
    I inserted a breakpoint on the first line after the "End if" statements, see here the entire If-Then block:
    That line has not been executed then? , though that variable should have been set by then.

    Any time you are repeating code, there is generally a better way.

    If I was writing that, as the variable will be mainly N (only one case of Y?), I would set that at the start of the tests to N, and only code for the Y.

    Please show us the DimVariables module.
    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

  9. #9
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,879
    I made the change to Option Explicit, and it didn't work.
    Just to be clear, at the top of every module you should have both
    Code:
    Option Compare Database
    Option Explicit
    not one or the other.

    Then you should compile your code by clicking on debug then Compile database in the vbe.
    This will show you if there are any errors in your code.

    I think we need to see the whole module not just the if-then portion.

    Where are you setting your variables?

    You may also want to consider using Public rather than Global.
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  10. #10
    June7's Avatar
    June7 is offline VIP
    Windows 11 Access 2021
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    moke, OP said these variables are Global and declared in a general module. They posted the code that is attempting to populate variable with issue.

    What advantage does Public have over Global?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  11. #11
    kdleaver1974 is offline Novice
    Windows 10 Access 2013 64bit
    Join Date
    Dec 2024
    Posts
    13
    Can't explain why, but when I change the Variable name from "FLAG" to "FLG" it works fine. Can't find a reason why such a change worked, but it did. I came up with the idea after doing various "tests" to see what was happening, and the fact when I used various different variable names, everything worked (I did a Replace all variables in entire Project).

    Thanks for the help anyways, and sorry for taking up everyone's time.

    Ken L

  12. #12
    kdleaver1974 is offline Novice
    Windows 10 Access 2013 64bit
    Join Date
    Dec 2024
    Posts
    13
    Quote Originally Posted by June7 View Post
    moke, OP said these variables are Global and declared in a general module. They posted the code that is attempting to populate variable with issue.

    What advantage does Public have over Global?
    IIRC, Public doesn't apply to the entire project, only Global seemed to work. At least having the variables in a General Module that is executed at the beginning when the program runs.

    Ken L

  13. #13
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,879
    Global is maintained for backwards compatibility. Public is the recommended keyword due to it's alignment with modern programming practices.
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  14. #14
    davegri's Avatar
    davegri is online now Excess Access
    Windows 11 Access 2021
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,740
    Public doesn't apply to the entire project, only Global seemed to work.
    Keep in mind that if a public or global variable is declared in a standard module and assigned a value in some procedure -
    If that variable name is Dimmed in any other procedure in your DB, that local variable just dimmed is used in that procedure. The global/public values in the standard module variable are ignored.

    So you should search the vba code for those variable names to see if they were re-Dimmed in another procedure.
    Last edited by davegri; 08-01-2025 at 01:56 PM. Reason: clarif

  15. #15
    June7's Avatar
    June7 is offline VIP
    Windows 11 Access 2021
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    davegri, doesn't quite explain why variable would have correct value in Debug but then following line showed it was empty.

    Ken, I tested Public and Global declarations in general module. Both were available for reference in other modules.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

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

Similar Threads

  1. Replies: 8
    Last Post: 01-27-2023, 07:01 AM
  2. Variable Remains Empty
    By dccjr3927 in forum Programming
    Replies: 5
    Last Post: 03-28-2019, 02:50 PM
  3. Replies: 1
    Last Post: 01-24-2019, 12:58 AM
  4. Replies: 17
    Last Post: 08-22-2017, 08:43 PM
  5. Replies: 4
    Last Post: 01-22-2015, 10:30 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