Page 1 of 2 12 LastLast
Results 1 to 15 of 19
  1. #1
    Amyfb is offline Advanced Beginner
    Windows 10 Office 365
    Join Date
    Mar 2021
    Posts
    56

    assigning variables - looking for a primer!

    I've watched or read hours of instruction on VBA and still run into walls when I try to write any functional code for my project.



    assigning variables is where I get tripped up , and for all I study example code, I'm not able to see a pattern that I could use as an outline to follow. I get that it is an alias for the real thing, and that multiple unique variables for the same object can exist. But putting that into practice is eluding me.

    Is there a pattern of any kind, or , is development really a matter of bouncing back and forth between the form control, the class module and the form module writing one or two little bits of code in a procedure , method or function each time? how do you all keep track of what is called what when and where?

    Does anyone have words of wisdom, a checklist or something to offer this fledgling developer? the toe bone's connected to the foot bone and the foot bone's connected to the ankle bone and the ankle bone's connected to the leg bone.....now that would be a nifty little memory method!

    thanks in advance for any tips!

    thanks
    Amyfb

  2. #2
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    9,470
    I get that it is an alias for the real thing
    No - perhaps that is part of your confusion. It represents a map/link to allocated memory. What you assign to the variable is stored there as its value, which could actually be an object, not just a piece of data.

    I cannot think of a reason why you'd use several variables for the same thing. Perhaps you can explain where/how you would for more insight. Actually, examples of your problem might help because it's not clear to me what the issue is. Maybe it's that you don't fully understand variable scope (can research that)?
    - "doesn't work" doesn't help. Implement changes in copies of your database.
    Iím telling everyone it's good to eat dried grapes. Itís all about raisin awareness.

  3. #3
    Amyfb is offline Advanced Beginner
    Windows 10 Office 365
    Join Date
    Mar 2021
    Posts
    56
    Quote Originally Posted by Micron View Post
    No - perhaps that is part of your confusion. It represents a map/link to allocated memory. What you assign to the variable is stored there as its value, which could actually be an object, not just a piece of data.

    I cannot think of a reason why you'd use several variables for the same thing. Perhaps you can explain where/how you would for more insight. Actually, examples of your problem might help because it's not clear to me what the issue is. Maybe it's that you don't fully understand variable scope (can research that)?
    ok i'll accept your map analogy to replace alias, and that helps a little.

    if I inspect some sample code carefully, I can eventually see that one variable gets written into a class module, a function and a procedure. I don't know how else to explain my confusion about organizing the development of the form.

    in the way intellisense gives prompts to writing a line of code, i'm looking for something that give prompts for assembling (?) the structure of the code parts.

    I'd like to have some kind of success before coming here for code review troubleshooting and this feels like such a basic thing that i need to wrap my head around.

    thanks for trying to help!

    cheerios
    Amyfb

  4. #4
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    9,470
    Class modules are a different animal altogether, what with their Property Let and Property Get statements. Now I'm thinking you might be referring to how what looks like a variable in some procedure triggers these Property statements and they use the same 'words' as some variable names. If that is the case, then when you refer to a variable (object) that Access recognizes as a custom class you're often referring to a property or method of that object, which causes the property statements to retrieve and assign the values. If that's not what you mean then sorry, I still don't follow you. Class modules are not for beginners IMO, so I would not call troubleshooting/understanding class code as being 'basic' when it's about a custom class. Form/report modules are classes as well, but I'm far more comfortable with those.
    - "doesn't work" doesn't help. Implement changes in copies of your database.
    Iím telling everyone it's good to eat dried grapes. Itís all about raisin awareness.

  5. #5
    Amyfb is offline Advanced Beginner
    Windows 10 Office 365
    Join Date
    Mar 2021
    Posts
    56
    Quote Originally Posted by Micron View Post
    Class modules are a different animal altogether, what with their Property Let and Property Get statements. Now I'm thinking you might be referring to how what looks like a variable in some procedure triggers these Property statements and they use the same 'words' as some variable names. If that is the case, then when you refer to a variable (object) that Access recognizes as a custom class you're often referring to a property or method of that object, which causes the property statements to retrieve and assign the values. If that's not what you mean then sorry, I still don't follow you. Class modules are not for beginners IMO, so I would not call troubleshooting/understanding class code as being 'basic' when it's about a custom class. Form/report modules are classes as well, but I'm far more comfortable with those.
    I wonder now if maybe the operative phrase for my issue is 'passing the variables'.

    Your point is absolutely valid about custom class modules for the moment. For now I'm just trying to be happy in a form module; keeping it simple, starterbee! ;-)

    The debugger is helping some - at times reminds me of something I made notes about, or studied, and it provides me with keywords to chase more info in my various e-resources.

    Sorry my explanations aren't more clear because every little bit helps and nearly always I do get some nugget of usefulness from your observations on my troubles.

    thanks
    Amyfb

  6. #6
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    9,470
    You're welcome, for whatever little light I shed on your issue (which to be honest, isn't 100% clear, but is that because of wine, Scotch, beer, old age or some combination thereof?)

    If it's about passing variables to procedures ByRef or ByVal then look for some example code and play with it? Perhaps Google ms access byRef vs byVal
    - "doesn't work" doesn't help. Implement changes in copies of your database.
    Iím telling everyone it's good to eat dried grapes. Itís all about raisin awareness.

  7. #7
    orange's Avatar
    orange is offline Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    15,688
    This may not be helpful but it is a definition/description from Google search.

    What is a variable and what does it represent?
    What is a variable from a computer programming view?

    In computer programming, a variable or scalar is a storage location (identified by a memory address) paired with an associated symbolic name, which contains some known or unknown quantity of information referred to as a value; or in easy terms, a variable is a container for a particular type of data (like integer, text....)


    You DIM (declare a variable of a certain type); then you assign it a value

    Dim intInitialValue as Integer ' this reserves a area in memory that will/may hold an Integer value
    intInitialValue =456 'this assigns the integer value 456 to that memory location

  8. #8
    davegri's Avatar
    davegri is online now Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    2,823
    Maybe your confusion arises from this:

    Click image for larger version. 

Name:	function.png 
Views:	31 
Size:	27.2 KB 
ID:	46209

    And here's the code if you want to copy/paste and experiment:

    Code:
    Public Function fcnIsWeekEnd(dDate As Date) As Boolean
        fcnIsWeekEnd = IIf(Weekday([dDate], vbSunday) = vbSaturday Or Weekday([dDate], vbSunday) = vbSunday, True, False)
    End Function
    
    
    
    
    Public Sub CheckForWeekend()
        Dim testdate As Date
        If fcnIsWeekEnd(Date - 1) Then
            MsgBox Date & " is weekend"
        Else
            MsgBox Date & " is not a weeekend"
        End If
        
        testdate = CDate("8/1/2021")
        If fcnIsWeekEnd(testdate) Then
            MsgBox testdate & " is weekend"
        Else
            MsgBox testdate & " is not a weeekend"
        End If
    End Sub

  9. #9
    Amyfb is offline Advanced Beginner
    Windows 10 Office 365
    Join Date
    Mar 2021
    Posts
    56
    Quote Originally Posted by davegri View Post
    Maybe your confusion arises from this:

    Click image for larger version. 

Name:	function.png 
Views:	31 
Size:	27.2 KB 
ID:	46209
    for sure you have zeroed right in on what makes me crazy!

    I'm getting a little closer to understanding what's going on (I think) by printing out the form I created, and now I'm writing three pieces of info for each control" Object name, returns value(), and assign value to.

    so, my starting option group is defined as:
    object name: fraSelected
    returns value: y/n
    assign value to: blnChoice

    and the options within the frame are:
    optNew
    optExistMajor
    optExistMinor

    return values = true/false

    assign values to: Function UpdateVersionNumber()
    'and this is as far as I have gotten.

    i'll play with your sample code and see what it does for me. I've spent the day on other stuff to clear my head and we'll see if any light crept in on this topic.

    thanks for the ideas!

    cheerios
    amyfb

  10. #10
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    9,470
    How can an option frame return yes or no with 3 options - unless 2 are false (or true) and the other is whatever remains? If that was the case, wouldn't one option be good enough for 2 of the choices?

    What is it that you don't understand about what davegri posted? Perhaps it is that a function that takes a parameter can accept any value or variable as long as what is being passed as the parameter value is correct for the data type. Perhaps it should be noted that Date in that example is not a variable - it is a function name, so never use Date as a name for anything.

    Function UpdateVersionNumber() << as written, this accepts no parameters so how are you assigning anything to the function? Or perhaps you meant the function isn't meant to accept any parameters, but is supposed to return (not assign) a calculation or similar value. The way you've created the function doesn't specify a return data type.
    - "doesn't work" doesn't help. Implement changes in copies of your database.
    Iím telling everyone it's good to eat dried grapes. Itís all about raisin awareness.

  11. #11
    Amyfb is offline Advanced Beginner
    Windows 10 Office 365
    Join Date
    Mar 2021
    Posts
    56
    Quote Originally Posted by Micron View Post
    How can an option frame return yes or no with 3 options - unless 2 are false (or true) and the other is whatever remains? If that was the case, wouldn't one option be good enough for 2 of the choices?
    I thought the frame having something selected was a condition I could test for, and then, the actual selection has branches.
    What is it that you don't understand about what davegri posted? Perhaps it is that a function that takes a parameter can accept any value or variable as long as what is being passed as the parameter value is correct for the data type. Perhaps it should be noted that Date in that example is not a variable - it is a function name, so never use Date as a name for anything.
    i'm trying to understand it enough that i can ask a question that returns a useful answer. my newbie status is not helping. The responses from folks like yourself and davegri have sent me back to my books to review the lessons I've already been through. but clearly, something is not sticking the way I need it to.

    Function UpdateVersionNumber() << as written, this accepts no parameters so how are you assigning anything to the function? Or perhaps you meant the function isn't meant to accept any parameters, but is supposed to return (not assign) a calculation or similar value. The way you've created the function doesn't specify a return data type.
    That function statement is just a placeholder in my outline.

    i'm still outlining. It's my general approach to things, and I"m here just trying to get it all listed so i can "see" the connections in a more graphic portrayal. so I can make sure I've got all the little bits lined up with their proper names and places.

    I'm going to immerse myself in the code snip from davegri and chew on it - literally if necessary - until I clear this hurdle in understanding.

    thanks again!
    amyfb

  12. #12
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    9,470
    I thought the frame having something selected was a condition I could test for, and then, the actual selection has branches.
    Sure can test, although I don't know what you mean by the selection having branches. Perhaps that is one way of saying you can code to deal with one of several possibilities that are determined by the selection. All I'm saying is that I wasn't grasping the logic of dealing with a yes/no result (i.e. there are only 2 possible outcomes) yet there could be 3 choices/option buttons.

    I suppose you realize that every option button in a group has to be assigned a unique value, and choosing an option passes a value to the frame. Suppose out of 3 options you have assigned values of 1, 2 and 3. One of those values gets passed to the frame upon selection. If you instead assign True to the first and false to the second, I believe that if you assign either true or false for the 3rd control then clicking one will cause the other with the same value to be selected. Also, Access will complain slightly when if you design it that way. If a primer on option groups wasn't warranted, then please just ignore my rambling.

    I hope my responses are not adding to your frustration, but if they suggest that you don't understand something you've read, please do quote whatever the book says if it's contrary. It's possible that I might need to read that chapter too! I'm only here to help (and it seems that help is not always appreciated), not confound or confuse anyone.

    Keep at it and I am sure you will continue to progress.
    - "doesn't work" doesn't help. Implement changes in copies of your database.
    Iím telling everyone it's good to eat dried grapes. Itís all about raisin awareness.

  13. #13
    davegri's Avatar
    davegri is online now Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    2,823
    Code in Post#8 is a bit misleading.

    Here's a more accurate version with a couple of comments

    Code:
    Public Function fcnIsWeekEnd(dDate As Date) As Boolean
        fcnIsWeekEnd = IIf(Weekday([dDate], vbSunday) = vbSaturday Or Weekday([dDate], vbSunday) = vbSunday, True, False)
    End Function
    
    
    
    
    Public Sub CheckForWeekend()
        Dim testdate As Date
        If fcnIsWeekEnd(Date - 1) Then      'Yesterday
            MsgBox (Date - 1) & " is weekend"
        Else
            MsgBox (Date - 1) & " is not a weekend"
        End If
        
        If fcnIsWeekEnd(Date + 2) Then      'Day after tomorrow
            MsgBox (Date + 2) & " is weekend"
        Else
            MsgBox (Date + 2) & " is not a weekend"
        End If
        
        testdate = CDate("8/1/2021")
        If fcnIsWeekEnd(testdate) Then
            MsgBox testdate & " is weekend"
        Else
            MsgBox testdate & " is not a weekend"
        End If
    End Sub

  14. #14
    Amyfb is offline Advanced Beginner
    Windows 10 Office 365
    Join Date
    Mar 2021
    Posts
    56
    Quote Originally Posted by davegri View Post
    Code in Post#8 is a bit misleading.

    Here's a more accurate version with a couple of comments

    the comments in the revised code did clarify one point for me. I've commented the original from post 8, to demonstrate what I think I know and highlight the fuzz.

    thanks in advance for sharing your knowledge and helping me learn.

    Code:
    'nameTheFunction(variable for a necessary value) function result type
    
    Public Function fcnIsWeekEnd(dDate As Date) As Boolean
        
    'state the function using the necessary variable
    'if the dDate variable supplied is = to saturday or sunday, then return true, else false
    
        fcnIsWeekEnd = IIf(Weekday([dDate], vbSunday) = vbSaturday Or Weekday([dDate], vbSunday) = vbSunday, True, False)
    End Function
    
    
    
    ' name the subroutine that will call the function
    
    Public Sub CheckForWeekend()
        
    'declare variable to capture the value that the function uses
    'testdate = dDate
    
        Dim testdate As Date
    
    'evalute the expression and assign the value to testdate variable
    'where is the date coming from in order to evaluate and populate the message box?
    
        If fcnIsWeekEnd(Date - 1) Then
            
            MsgBox Date & " is weekend"
        Else
            MsgBox Date & " is not a weeekend"
        End If
     
     'testdate variable assigned a new value, that is converted into Date data type
     'the value of the variable testdate is hard coded here.
     
        testdate = CDate("8/1/2021")
        
     'the function evaluates the value of testdate and displays the message
    'but why does this have to be coded two times? 
    
        If fcnIsWeekEnd(testdate) Then
            MsgBox testdate & " is weekend"
        Else
            MsgBox testdate & " is not a weeekend"
        End If
    End Sub

  15. #15
    davegri's Avatar
    davegri is online now Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    2,823
    'where is the date coming from in order to evaluate and populate the message box?
    That's the system date.


    In the immediate window,
    ?Date
    ?Date - 1
    ?Date + 2

    and so on.

Page 1 of 2 12 LastLast
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. Assigning values to variables
    By missniffy in forum Programming
    Replies: 3
    Last Post: 02-21-2018, 03:09 PM
  3. Assigning a variable
    By tguitard in forum Programming
    Replies: 1
    Last Post: 05-02-2013, 12:22 PM
  4. Replies: 29
    Last Post: 06-19-2012, 12:12 PM
  5. Assigning value to an image
    By cdzajp in forum Forms
    Replies: 1
    Last Post: 06-16-2011, 12:05 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