Results 1 to 7 of 7
  1. #1
    NiMora is offline Novice
    Windows 8 Access 2013
    Join Date
    Jun 2015
    Location
    Germany
    Posts
    17

    sql statement INSERT and UPDATE withn one if - end if code


    and again. I know itīs late and I should go to bed but I want solve this part. So if you can take an eye on this.

    as in some threads before I have the following code:

    Code:
    Option Compare
    DatabaseOption Explicit
    
    
    Private Sub Befehl17_Click()
    ' ##################### OutlookImport ##################################
    Dim db As DAO.Database
    Dim rs As DAO.Recordset
    Dim outObject, Mapi, Inbox, InboxImported
    Dim i As Integer
    Dim strSQL As String
    
    
    Set rs = CurrentDb.OpenRecordset("OutlookImport")
    Set outObject = CreateObject("Outlook.Application")
    Set Mapi = outObject.GetNamespace("MAPI")
    Set Inbox = Mapi.GetDefaultFolder(olFolderInbox).Folders("import")
    Set InboxImported = Mapi.GetDefaultFolder(olFolderInbox).Folders("imported")
    
    
        For Each Mail In Inbox.Items
        
        strSQL = "INSERT INTO OutlookImport (AbsenderMail, AbsenderName, SendTo, SendCC, Betreff, MailDatum, Nachricht, EntryID) VALUES ('" & Mail.SenderEmailAddress & "', '" & Mail.SenderName & "', '" & Mail.To & "', '" & Mail.CC & "', '" & Mail.Subject & "', '" & Mail.SentOn & "', '" & Mail.Body & "', '" & Mail.EntryID & "');"
    
    
            DoCmd.SetWarnings False
            On Error Resume Next
            DoCmd.RunSQL strSQL
            DoCmd.SetWarnings True
        
             If Not Err.Number <> 0 Then
                If Mail.Attachments.Count > 0 Then                
                    AnzAttach = Mail.Attachments.Count
                    strPath = "C:\Unterlagen\" & Replace(Mail.SenderName, " ", "") & "_" & Replace(Replace(Mail.SentOn, " ", "_"), ":", ".")
                    Debug.Print strPath
                    MkDir strPath
                    
                    For i = 1 To AnzAttach
                        Mail.Attachments.Item(i).SaveAs strPath & Mail.Attachments.Item(i).FileName
                    Next i
    
                    strSQLAtt = "UPDATE OutlookImport SET AttachFolder = '" & strPath & "' WHERE AbsenderMail = '" & Mail.SenderEmailAddress & "'"
                    CurrentDb.Execute strSQLAtt
    
                End If                    
                Mail.Move InboxInBearbeitung 'Mails verschieben in Bearbeitung
             End If
        Next
    
    
        Forms![OutlookImport].Requery
    End Sub
    I want to import the path to the Attachments I import before or put into a folder
    I add an UPDATE sql statement but it donīt work correctly. I get on every record the same string (the same folder).

    what is wrong?

  2. #2
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    It is probably a syntax error. I would make sure everything is printing out as expected using something like the following ...

    Code:
    
    'Dim db As DAO.Database
    'Dim rs As DAO.Recordset
    Dim outObject, Mapi, Inbox, InboxImported
    Dim i As Integer
    Dim strSQL As String
    
    'Set rs = CurrentDb.OpenRecordset("OutlookImport")
    Set outObject = CreateObject("Outlook.Application")
    Set Mapi = outObject.GetNamespace("MAPI")
    Set Inbox = Mapi.GetDefaultFolder(olFolderInbox).Folders("import")
    'Set InboxImported = Mapi.GetDefaultFolder(olFolderInbox).Folders("imported")
    
        For Each Mail In Inbox.Items
        
    '    strSQL = "INSERT INTO OutlookImport (AbsenderMail, AbsenderName, SendTo, SendCC, Betreff, MailDatum, Nachricht, EntryID) VALUES ('" & Mail.SenderEmailAddress & "', '" & Mail.SenderName & "', '" & Mail.To & "', '" & Mail.CC & "', '" & Mail.Subject & "', '" & Mail.SentOn & "', '" & Mail.Body & "', '" & Mail.EntryID & "');"
    '
    '
    '        DoCmd.SetWarnings False
            On Error Resume Next
    '        DoCmd.RunSQL strSQL
    '        DoCmd.SetWarnings True
        
             If Not Err.Number <> 0 Then
                If Mail.Attachments.Count > 0 Then
                    AnzAttach = Mail.Attachments.Count
                    strPath = "C:\Unterlagen\" & Replace(Mail.SenderName, " ", "") & "_" & Replace(Replace(Mail.SentOn, " ", "_"), ":", ".")
                    Debug.Print "*******************************************"
                    Debug.Print strPath
    '                MkDir strPath
                    
    '                For i = 1 To AnzAttach
    '                    Mail.Attachments.Item(i).SaveAs strPath & Mail.Attachments.Item(i).FileName
    '                Next i
                    strSQLAtt = "UPDATE OutlookImport SET AttachFolder = '" & strPath & "' WHERE AbsenderMail = '" & Mail.SenderName & "'"
                    Debug.Print "**" & vbCrLf
                    Debug.Print strSQLAtt
                    Debug.Print "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"
    '                CurrentDb.Execute strSQLAtt
                End If
    '            Mail.Move InboxInBearbeitung 'Mails verschieben in Bearbeitung
             End If
        Next
    
    '    Forms![OutlookImport].Requery

  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
    52,929
    In your other thread I thought you changed the code to include another \ character and use SaveAsFile?

    That UPDATE should work. Step debug. Make sure the WHERE clause is correct. Review link at bottom of my post for debugging guidelines.
    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
    NiMora is offline Novice
    Windows 8 Access 2013
    Join Date
    Jun 2015
    Location
    Germany
    Posts
    17
    Hi ItsMe, thx for reply.

    I have allreade do this. I check all variables and its content. actually it works tecnicaly, but I get allways the same content in this field. view moments ago I try to use MailDatum and Mail.SentOn instead of Mail.SenderEmailAddress. with these I get no content in the fields.

    could you show me maybe another sollution, to put the path-string into the field "AttachFolder"? Iīm a litte desperately

  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
    52,929
    Did you read post 3?

    Did you try the Debug.Print code suggested by ItsMe?
    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
    NiMora is offline Novice
    Windows 8 Access 2013
    Join Date
    Jun 2015
    Location
    Germany
    Posts
    17
    Nope, june,

    Sorry for that, that was a copy paste Problem. I've changed the Code to SaveAsFile.

    but it's 1 AM in Germany, I go to bed now and try my best tomorrow, to find a solution.

    thx 4 your help

  7. #7
    NiMora is offline Novice
    Windows 8 Access 2013
    Join Date
    Jun 2015
    Location
    Germany
    Posts
    17
    hey, sorry for my late reply. I did not solve the problem, I put it all in the INSERT statement.

    thx for help

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

Similar Threads

  1. Replies: 3
    Last Post: 05-27-2015, 01:04 PM
  2. Replies: 2
    Last Post: 03-11-2015, 07:48 AM
  3. Replies: 1
    Last Post: 10-29-2014, 10:19 AM
  4. Replies: 2
    Last Post: 03-07-2014, 09:40 PM
  5. Update Statement in form code
    By ksmith in forum Programming
    Replies: 9
    Last Post: 11-07-2011, 12:04 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