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

    Multiple fields in a function

    I really seem to be struggling with the basics here.



    I am trying tio run a sub with three variables, and as soon as I add more than one the string turns red and will not work.

    Code:
    SendMessage (FileSavePath, Forms!frmTimeCard.txtDate.Value, Forms!frmTimeCard.txtDate.Value)
    In my function, I am feeding the data in, I think correctly...

    Code:
    Private Sub SendMessage(FileSavePath As String, SD As Date, ED As Date)
        Dim appOutLook As Outlook.Application
        Dim MailOutLook As Outlook.MailItem
    
    
        Dim txtempname As String
        ' txtempname = Nz(DLookup("name", "dbo_empbasic", "empid = '" & txtEmpID & "' "), "")
        ' txtempname = GetUserName(empid)
        
        Dim sql As String
        sql = "SELECT dbo_empbasic.name FROM dbo_empbasic WHERE dbo_empbasic.empid = '" & empid & "' "
        
        Dim rs As ADODB.recordset
        Set rs = New ADODB.recordset
        
        rs.Open sql, CurrentProject.Connection, adOpenDynamic, adLockOptimistic
        rs.MoveFirst
        
        txtempname = rs.Fields("name")
        
        rs.Close
        Set rs = Nothing
        
        
        Debug.Print ("empid: " & empid)
        Debug.Print ("txtempname: " & txtempname)
        
        Set appOutLook = CreateObject("Outlook.Application")
        Set MailOutLook = appOutLook.CreateItem(olMailItem)
    
    
        With MailOutLook
            .BodyFormat = olFormatRichText
            .To = "sanderson@metromachine.com"
            ''.cc = ""
            ''.bcc = ""
            .Subject = "Weekly Timecard for " & txtempname & "."
            .HTMLBody = "Attached is the timecard for " & txtempname & ", employee number " & empid & " for the week of " & CStr(SD) & " through " & CStr(ED) & "."
            .Attachments.Add (FileSavePath)
            .Send
            '.Display    'Used during testing without sending (Comment out .Send if using this line)
                
        End With
      
    End Sub
    Also, within this code, I am trying to run a function called GetUserName. I've actually tried a couple of ways to populate the variable 'txtempname', but it doesn't work. So, I have just sloppily copied the information into this sub, abandoning the modular approach to get it to work.



    Code:
    Private Function GetUserName(empid As String) As String
        Dim sql As String
        sql = "SELECT dbo_empbasic.name FROM dbo_empbasic WHERE dbo_empbasic.empid = '" & empid & "' "
        
        Dim rs As ADODB.recordset
        Set rs = New ADODB.recordset
        
        rs.Open sql, CurrentProject.Connection, adOpenDynamic, adLockOptimistic
        rs.MoveFirst
        
        Dim fName As String
        fName = rs.Fields("name")
        
        Debug.Print ("fName: " & fName)
    
    
        rs.Close
        Set rs = Nothing
        
    End Function
    Perhaps I'm just not used to VBA anymore. I believe a return must be used in .NET, but in this it just returns the last variable defined.

    Thanks for the help!

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,550
    you dont need the .value, and keep all exclamations in the path
    Forms!frmTimeCard!txtDate

  3. #3
    sanderson is offline Advanced Beginner
    Windows 8 Access 2013
    Join Date
    Jul 2015
    Posts
    64
    Okay, but this does not solve the problem. The text in the editor shows up red as soon as I add more than one variable in the sub call.

  4. #4
    sanderson is offline Advanced Beginner
    Windows 8 Access 2013
    Join Date
    Jul 2015
    Posts
    64
    Code:
    SendMessage (FileSavePath, Forms!frmTimeCard!txtDate, Forms!frmTimeCard!txtDate)
    now returns the error "Expected ="

  5. #5
    DrGUI is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Aug 2015
    Location
    Parts Unknown
    Posts
    23
    Remove the parens:

    SendMessage FileSavePath, Forms!frmTimeCard!txtDate, Forms!frmTimeCard!txtDate

    For your function, add the following before the end of the function:

    GetUserName = fName

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

Similar Threads

  1. Replies: 7
    Last Post: 05-28-2015, 11:43 AM
  2. Replies: 1
    Last Post: 08-26-2013, 05:08 PM
  3. Replies: 8
    Last Post: 08-02-2012, 10:50 AM
  4. Replies: 12
    Last Post: 05-07-2012, 12:41 PM
  5. total many fields by function
    By thetuyen in forum Access
    Replies: 2
    Last Post: 03-08-2010, 08:43 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