Results 1 to 9 of 9
  1. #1
    sanderson is offline Advanced Beginner
    Windows 8 Access 2013
    Join Date
    Jul 2015
    Posts
    64

    How to do a function in VBA

    I think this is a silly question, because I think I did it right. However, the variable txtDept returns null. strDept returns a correct value. All I want to do is run the function and assign its returned value to a variable.



    Thanks!

    Code:
    Private Sub txtLaborHours_AfterUpdate()
        Dim txtEmpID As String
        txtEmpID = Forms!frmTimeCard.txtEmpID.Value
        
        Dim txtDept As String
        txtDept = GetDeptStatus(txtEmpID)
        Debug.Print ("txtDept: " & txtDept)
        
        txtNotes.SetFocus
        Select Case txtDept
            Case "ME", "EE", "AS", "MA", "HYD"
                If Len(txtNotes.Text) = 0 And cboBillable.Value = "I" Then
                    txtLaborHours.Value = Null
                    txtNotes.SetFocus
                    Me.Refresh
                    MsgBox "You must enter Notes on non-billable activity before Time. "
           
                End If
            
        End Select
           
    End Sub
    Code:
    Private Function GetDeptStatus(strEmpID As String) As String
        ' Added 9/22/15
        ' This sub gets the Shop Supervisor Status from Vantage to determine which controls to show or hide
        
        ' Dimension variables
        Dim sqlGetDeptStatus As String
        Dim rsGetDeptStatus As adodb.recordset
        
        ' Set variables
        Set rsGetDeptStatus = New adodb.recordset
        
        Debug.Print ("strEmpID: " & strEmpID)
            
        ' Create Query
        sqlGetDeptStatus = "SELECT dbo_empbasic.jcdept " & _
                                 "FROM dbo_empbasic " & _
                                 "WHERE dbo_empbasic.empid = '" & strEmpID & "' "
                        
        Debug.Print ("sqlGetDeptStatus: " & sqlGetDeptStatus)
                        
        ' Open the recordset and populate the form
        rsGetDeptStatus.Open sqlGetDeptStatus, CurrentProject.Connection, adOpenDynamic, adLockOptimistic
        
        Dim strDept As String
        
        If Not rsGetDeptStatus.EOF Then
            strDept = rsGetDeptStatus.Fields("jcdept")
            
        Else
            strDept = ""
            
        End If
           
        rsGetDeptStatus.Close
        Set rsGetDeptStatus = Nothing
        
        strDept = strDept
        Debug.Print ("strDept: " & strDept)
        
    End Function

  2. #2
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 8 Access 2013
    Join Date
    Aug 2013
    Posts
    7,862
    What is the following for? Is this the value you are trying to assign to your function?

    Code:
        strDept = strDept
        Debug.Print ("strDept: " & strDept)

  3. #3
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    Example of a custom function and calling that function to set a variable.

    Function MyFun()
    MyFun = "Test"
    End Function

    x = MyFun()

    The following makes no sense:
    strDept = strDept

    Your function lacks the line that sets the function value to pass back to the calling procedure.

    GetDeptStatus = strDept

    Do you really need a custom function? Could intrinsic DLookup() accomplish?

    DLookup("jcdept", "dbo_empbasic", "empid = '" & Me.EmpID & "'")

    Is EmpID a text type field? If it is number type, then remove the apostrophe delimiters.
    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.

  4. #4
    sanderson is offline Advanced Beginner
    Windows 8 Access 2013
    Join Date
    Jul 2015
    Posts
    64
    I am fine with removing strDept = strDept because...
    strDept = rsGetDeptStatus.Fields("jcdept")

    I added this at the end so see if it would help, because I thought possibly the last item defined would make it work.

  5. #5
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    Did you try either of the changes I suggested?
    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
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 8 Access 2013
    Join Date
    Aug 2013
    Posts
    7,862
    Quote Originally Posted by sanderson View Post
    I am fine with removing strDept = strDept because...
    strDept = rsGetDeptStatus.Fields("jcdept")

    I added this at the end so see if it would help, because I thought possibly the last item defined would make it work.
    OK, check out the following revision and see if it makes sense. You will still want to change some additional things. However, this should work as is.


    Code:
        ' Added 9/22/15
        ' This sub gets the Shop Supervisor Status from Vantage to determine which controls to show or hide
        
        ' Dimension variables
        Dim sqlGetDeptStatus As String
        Dim rsGetDeptStatus As adodb.Recordset
        
        ' Set variables
        Set rsGetDeptStatus = New adodb.Recordset
        GetDeptStatus = ""
        
        Debug.Print ("strEmpID: " & strEmpID)
            
        ' Create Query
        sqlGetDeptStatus = "SELECT dbo_empbasic.jcdept " & _
                                 "FROM dbo_empbasic " & _
                                 "WHERE dbo_empbasic.empid = '" & strEmpID & "' "
                        
        Debug.Print ("sqlGetDeptStatus: " & sqlGetDeptStatus)
                        
        ' Open the recordset and populate the form
        rsGetDeptStatus.Open sqlGetDeptStatus, CurrentProject.Connection, adOpenDynamic, adLockOptimistic
        
        Dim strDept As String
        
        If Not rsGetDeptStatus.EOF Then
    '        strDept = rsGetDeptStatus.Fields("jcdept")
            GetDeptStatus = rsGetDeptStatus.Fields("jcdept")
            rsGetDeptStatus.Close
            
    '    Else
    '        strDept = ""
            
        End If
           
    '    rsGetDeptStatus.Close
        Set rsGetDeptStatus = Nothing
        
    '    strDept = strDept
    '    Debug.Print ("strDept: " & strDept)
         Debug.Print ("GetDeptStatus: " & GetDeptStatus)

  7. #7
    sanderson is offline Advanced Beginner
    Windows 8 Access 2013
    Join Date
    Jul 2015
    Posts
    64
    Thanks. Brings back memories before the .NET days.

  8. #8
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 8 Access 2013
    Join Date
    Aug 2013
    Posts
    7,862
    Yah there is not 'Return' with VBA so you use the name of the function. I would recommend matching the data types as best as possible. In other words, you explicitly declare some variables as strings. If a string is assigned a number it will be a string. If a field is of type Number and you use a string variable as an assignment to the field, Access will automatically cast the string to number (if possible). I do not know what you are using for Unique Identifiers but I would consider type Long as a variable declaration.

  9. #9
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    Did you see post 3?
    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.

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

Similar Threads

  1. Replies: 8
    Last Post: 11-04-2014, 10:44 PM
  2. Replies: 8
    Last Post: 01-31-2014, 01:45 PM
  3. function
    By azhar2006 in forum Queries
    Replies: 6
    Last Post: 12-14-2013, 10:35 AM
  4. Max function???
    By kaite07 in forum Queries
    Replies: 1
    Last Post: 11-10-2010, 05:20 PM
  5. Want function to get current function name
    By Davis DeBard in forum Programming
    Replies: 2
    Last Post: 08-13-2009, 05:02 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