Results 1 to 12 of 12
  1. #1
    d9pierce1 is offline Expert
    Windows 10 Access 2016
    Join Date
    Jan 2012
    Location
    Oklahoma
    Posts
    754

    General Questions on declorations

    Hi all,


    I am trying to figure out (Understand) declorations. The following code sample below will use to illistrate my questions!

    In the public function there are items (sLast As string, ....) and i am curious as they appear to just be Dim to me? Why put them in this line?
    I guess i am asking why on this example, do you use the (sLast As String, ... ) and then use Dim in the code below it?
    Last, debug.print is only in there for the immediate window correct? Other than that it doesnt do anything correct?

    Thanks for all the help! I am trying to uderstand these things.
    dave

    Code:
    Option Compare DatabaseOption Explicit
    
    
    Public Function fcnFullName(sLast As String, sFirst As String, bUseMaiden As Boolean, bUseMaidenLast As Boolean, _
    Optional sMaiden As String = "", Optional sMiddle As String = "")
        
    'GoodFullName: fcnFullName([LastName],[FirstName],[UseMaiden],[UseMaidenLast],[MaidenName],[MiddleName])
        
        On Error GoTo Error_Handler
        Dim sRslt As String
        Dim LN As String
        Dim MI As String
        If Len(Trim(sMiddle)) > 0 Then MI = Left(sMiddle, 1)
        sRslt = Trim(sFirst) & Space(1) & MI & Space(1)
        Debug.Print sRslt
        LN = sLast
        If bUseMaiden = True Then
            LN = Trim(sMaiden)
        ElseIf bUseMaidenLast = True Then
            LN = Trim(sMaiden) & "-" & Trim(sLast)
        End If
        fcnFullName = sRslt & LN
    Error_Handler_Exit:
        On Error Resume Next
        Exit Function
    Error_Handler:
        Select Case Err
            Case Else
                MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure fcnFullName" & "."
        End Select
        Resume Error_Handler_Exit
    End Function

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,815
    sLast, sFirst, etc are arguments of the function declaration. This a way to pass values into the function when the function is called.

    The Dim statements declare variables within the function it will use to manipulate data passed to it.

    Correct about Debug.
    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
    d9pierce1 is offline Expert
    Windows 10 Access 2016
    Join Date
    Jan 2012
    Location
    Oklahoma
    Posts
    754
    I have one more question!
    Where do the letters one uses in code come from such as the fcn in fcnFullName = sRslt & LN ? I have seen things like Dim blnValid As Boolean in some of my code samples and jsut have to wonder if this is just a personal
    preference call or do they actually mean somehting. Like rs or rst for recordset, LN for last name, and why are most of them lower case?
    Dim blnValid As Boolean
    Dim blnValidCriteria As Boolean
    Dim intChar As Integer
    Thanks
    Dave


  4. #4
    d9pierce1 is offline Expert
    Windows 10 Access 2016
    Join Date
    Jan 2012
    Location
    Oklahoma
    Posts
    754
    OK, now that makes sense. I have been doing some reading on these things and was confused.
    Thanks

  5. #5
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,815
    Use mostly whatever you want for creating variable names (don't begin with numbers or use symbols/punctuation). Use of prefix like fcn is personal preference. Some are commonly understood: str means String, dbl means Double, int means Integer, etc. Common practice to lowercase prefix.

    This is a topic of many discussions.
    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.

  6. #6
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,737
    The subject is a very broad one with many approaches, some more popular than others. Best that you find one you like and adopt it, and try not to be too dissimilar to everyone else lest you find your code is too difficult to follow. The upper / lower case thing is oft referred to as camel case - sortOfLikeHumpsBetweenWords. That way, you can easily (theoretically) understand the separation of data and object types from their names. So I will use things like
    lst > ListBox
    cmb > ComboBox
    txt > Textbox etc.
    as control object prefixes
    and
    dbl, sng, int, lng, bol, str for double, single, integer, long, boolean and string.

    After reading 20 or more lines down someone's code, it's nice to not have to go back to the top to figure out what LN is, which is what I'd likely have to do when reading your example code.

    here's a couple of treatises on naming conventions

    http://theaccessweb.com/general/gen0012.htm
    https://www.devhut.net/2017/12/21/naming-conventions/

    and one on what not to use in names
    http://www.allenbrowne.com/AppIssueBadWord.html

    In summary, if you prefix every variable/object name you will never use a reserved word. Can't say the same for things like field names though. I typically would not use strName for a table field, but I wouldn't use Name either, and that only comes from experience.

    EDIT - BTW, I would not use LN for last name; it's too cryptic. To others it might mean Line Number or even Liquid Nitrogen (I presume that will illustrate the point) but regardless, it doesn't reveal the data type as the name value could be a long (primary key from the parent table) or a string. I'd use LName and FName for the fields and strLname and strFname for variables if they are strings (lngFname if it is a long). Sometimes you can be too frugal with your letters when coding, and to some, those names might be an example of that. I think it beats LN though.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  7. #7
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2016
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,716

  8. #8
    d9pierce1 is offline Expert
    Windows 10 Access 2016
    Join Date
    Jan 2012
    Location
    Oklahoma
    Posts
    754
    thanks all,
    Very nice info and links too.
    I only had one question after viewing these things today and that was on arguements.
    So, how does one know when an arguement is needed. I am not being funny, i really dont know!
    I mean, what prompts one to know that an arguement is needed.
    On my first post on this segment, obvious that i didnt write that, actually DaveGri wrote that for me!
    He is a really nice person and has been so helpful but in any event, he had lots of arguements and i was
    just wondering what prompted him to know he needed all those arguements for this function?

    Thanks All,
    Dave

  9. #9
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,643
    A lot depends on what you want to do. You use arguments to pass info to your procedure to make it do what you want.
    for instance lets say you wanted a function to concatenate a clients name so you can use it anywhere in your project but sometimes you want the first name, middle name, last name and suffix and other times you want to have the last name first. Instead of using 2 functions you can pass an argument so the one function can do both.
    ie. Dave Pierce or Pierce, Dave.
    you could do something like this.

    Code:
    Public Function ClientName(lngClientID As Long, Optional LastNameFirst As Boolean = False) As String
    
        Dim db As DAO.Database
        Dim rs As DAO.Recordset
        Dim strSql As String
        Dim strOut As String
        strSql = "select * from  tblClients where ClientID = " & lngClientID
        Set db = CurrentDb()
        Set rs = db.OpenRecordset(strSql)
    
        If LastNameFirst = False Then
    
            ClientName = (rs!FirstName + " ") & (rs!MiddleName + " ") & (rs!LastName) & (" " + rs!Suffix)
    
        Else
    
            ClientName = (rs!LastName + ", ") & (rs!FirstName + " ") & (rs!MiddleName + " ") & (rs!Suffix)
    
        End If
    
    MyExit:
        rs.Close
        Set rs = Nothing
        Set db = Nothing
    
    End Function
    if I wanted Dave M. Pierce Jr. whose primary key is 25, i could call it with
    x = ClientName(25, false) or x = ClientName(25). The optional argument is set to default to false (Optional LastNameFirst As Boolean = False) so it can be omitted from the call if you want to use its default. In the vbe intellisense will show brackets around an optional argument.

    if I wanted Pierce,Dave M. Jr. I could call it with
    x = ClientName(25,true)

    In DaveGri's example you posted he's just doing a lot more where he needs to pass more arguments to the procedure.
    This is just a brief explanation as there's a lot more to it and its worth doing some googling about it.
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  10. #10
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,737
    how does one know when an arguement is needed
    Meaning what, exactly? Needed in terms of structuring the function so that it does what you want it to do? Or needed as in must provide an argument value because doing so is not an option when calling the function?

    The former would be governed by you designing the function to do what you want it to do, which dictates what it is you think you need to pass to it.
    The latter is indicated by [brackets] as Moke123 says, around the optional parts and this is indicated by Intellisense. That's the prompt you get from Access vba which helps you complete a lot of functions, whether built in or user defined functions (UDF).

    One last note about functions. There are 2 situations where a function call must be wrapped in parentheses:
    - if the function is to return a value
    - if you use the Call statement (which I virtually never do)

    If you use parentheses outside of these conditions you will raise an error. So
    Dim intResult As Integer
    intResult = MsgBox(arguments here) <- returns an integer value dependent upon which button user clicks in message box
    is OK because the message box function can (and in this case, does) return a value.

    MsgBox(arguments here) is not OK.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  11. #11
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2016
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,716
    Dave,

    Have you reviewed this? It may help put your question into context. I have found that by reviewing and (adjusting/experimenting) an established function(s), you can do some trial and error to "learn" about functions.

  12. #12
    d9pierce1 is offline Expert
    Windows 10 Access 2016
    Join Date
    Jan 2012
    Location
    Oklahoma
    Posts
    754
    thank you, I will do some experimenting this weekend with this.
    Dave

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

Similar Threads

  1. General Questions
    By Dave14867 in forum Access
    Replies: 14
    Last Post: 10-22-2018, 07:06 PM
  2. A few general questions...
    By Daryl2106 in forum Access
    Replies: 3
    Last Post: 02-29-2012, 09:57 PM
  3. General Access questions - can or cannot
    By Armitage2k in forum Access
    Replies: 2
    Last Post: 11-28-2011, 07:28 PM
  4. Querry general questions
    By newtoAccess in forum Access
    Replies: 2
    Last Post: 04-04-2011, 06:56 PM
  5. General Questions about a Switchboard
    By yes sir in forum Access
    Replies: 2
    Last Post: 09-23-2010, 11:28 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