Results 1 to 10 of 10
  1. #1
    Ruegen's Avatar
    Ruegen is offline VIP
    Windows 8 Access 2010 64bit
    Join Date
    Jul 2013
    Location
    Australia
    Posts
    1,496

    Cycle through variables


    Is there a quick way in vba code to cycle through a list of variants i.e. var1, var2, var3 and set them all to null

    or do I have to

    var1 = null
    var2 = null

    I'd like to group it all and also do things like check if they are null too.

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    AFAIK, must address each individually, cannot 'cycle' variables.

    Unless there is some very advanced code I've never encountered, cannot dynamically build a variable name. This won't work:

    For i = 1 to 10
    "var" & i = Null
    Next
    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
    Ruegen's Avatar
    Ruegen is offline VIP
    Windows 8 Access 2010 64bit
    Join Date
    Jul 2013
    Location
    Australia
    Posts
    1,496
    I don't want to build a variable name - I already have public variables.

    I'll explain the situation.

    I've made a new class which has many variables and a few methods, the last saving to the database using dao. But I encountered an issue that if I gave a data type to the variables like say date then I would get a variable with a date whether I had set one or not when calling and setting the class in any sub. This means I can't use if is null - I wanted it so that before the save method on the class could be used it would check through each var in the newly made class and made sure there are no missing data.

    e.g.

    Code:
    dim Booking as BookingClass
    set Booking = new bookingclass
    
    booking.init
    booking.BookingDate = now()
    booking.ShowID = 4
    
    if booking.verify = false then
    else
    booking.save
    end if
    so I found I couldn't check using isEmpty, isnull, isnothing etc because as soon as you make the class object you init the variables.

    So I swapped each type to variant to hold null then use an .init method to null each of them before you can use them (there is a bool in the init method that gets set to true and if true all other methods etc will work)

    so there are a lot of vars in the .init method and I would rather have something like

    var1, varf, varxyz, varbooking, varstatusid = null

    Currently I have to

    var1 = null
    varf = null
    varxyz = null

    etc

    I'd rather just group them all some how and set to null.

  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    Doesn't change my comments.
    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
    Ruegen's Avatar
    Ruegen is offline VIP
    Windows 8 Access 2010 64bit
    Join Date
    Jul 2013
    Location
    Australia
    Posts
    1,496
    Quote Originally Posted by June7 View Post
    Doesn't change my comments.
    *sadface*

  6. #6
    thebigthing313 is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jan 2014
    Posts
    119
    I know of no way to indirectly reference a variable as well. However, the issue that brought you here in the first place possibly can be addressed?

    You can try writing a custom IsNull type function, and using VarType to test dates, longs, doubles, ints, etc against 0, strings against "", etc.

  7. #7
    Ruegen's Avatar
    Ruegen is offline VIP
    Windows 8 Access 2010 64bit
    Join Date
    Jul 2013
    Location
    Australia
    Posts
    1,496
    Quote Originally Posted by thebigthing313 View Post
    I know of no way to indirectly reference a variable as well. However, the issue that brought you here in the first place possibly can be addressed?

    You can try writing a custom IsNull type function, and using VarType to test dates, longs, doubles, ints, etc against 0, strings against "", etc.
    Yeah I already thought of doing that, putting dates to a date no one would use like 1/1/1950 however that's cumbersome....

  8. #8
    Ruegen's Avatar
    Ruegen is offline VIP
    Windows 8 Access 2010 64bit
    Join Date
    Jul 2013
    Location
    Australia
    Posts
    1,496
    I've provided some code below, it's a function that checks variants and returns a bool

    Code:
    'is variable null (any data type)
    Public Function isNullVar(variable As Variant) As Boolean
    
    
    isNullVar = False
    
    
        Select Case VarType(variable)
            
            'null check
            Case 0, 1
            isNullVar = True
            
            'number check
            Case 2, 3, 4, 5, 6
            If variable <= 0 Then
            isNullVar = True
            End If
            
            'date check
            Case 7
            If variable <= #1/1/1999# Then
            isNullVar = True
            End If
                    
            'string check
            Case 8
            If Trim(variable) = "" Then
            isNullVar = True
            End If
                
        End Select
    
    
    End Function

  9. #9
    amrut is offline Expert
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2012
    Location
    Dubai
    Posts
    614
    Can't you use a dynamic array ?
    Dim var() as variant

  10. #10
    Ruegen's Avatar
    Ruegen is offline VIP
    Windows 8 Access 2010 64bit
    Join Date
    Jul 2013
    Location
    Australia
    Posts
    1,496
    Quote Originally Posted by amrut View Post
    Can't you use a dynamic array ?
    Dim var() as variant
    I could store variable into an array and make a loop but then I still have to place them into the array.... which means I'm coding them in.... the point is less coding...?

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

Similar Threads

  1. Cycle Through Parameters in VBA
    By kestefon in forum Access
    Replies: 3
    Last Post: 07-10-2014, 02:27 PM
  2. Replies: 7
    Last Post: 10-28-2013, 03:15 PM
  3. Recordset cycle problem
    By free_style in forum Programming
    Replies: 3
    Last Post: 08-25-2011, 02:44 PM
  4. Cycle Text Boxes with a For next loop
    By Gary in forum Programming
    Replies: 3
    Last Post: 07-20-2010, 09:32 AM
  5. Cycle Time
    By Dargo in forum Forms
    Replies: 5
    Last Post: 02-26-2009, 05:14 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