Results 1 to 10 of 10

Thread: Excel and Access VBA

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2018
    Posts
    5

    Excel and Access VBA

    I am trying to save an attachment into Access DB using Excel VBA. Do anyone know how to do it.

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Excel and Access VBA

    is the attachment plain text or binary data?
    what type of field do you have in the database?
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2018
    Posts
    5

    Re: Excel and Access VBA

    Quote Originally Posted by westconn1 View Post
    is the attachment plain text or binary data?
    what type of field do you have in the database?
    If you right click on Access fields you can see there is Attachment field. So I have created Attachment field in Access and now I want to save different files from Excel VBA to Access. When you select the file from Excel and open it then that file should be saved in Access directly

  4. #4

    Thread Starter
    New Member
    Join Date
    Apr 2018
    Posts
    5

    Re: Excel and Access VBA

    This code is to open file

    Dim cnn As ADODB.Connection
    Dim rst As ADODB.Recordset
    Dim fd As FileDialog
    Dim var As Boolean
    Set cnn = New ADODB.Connection
    Set fd = Application.FileDialog(msoFileDialogOpen)
    fd.Filters.Clear
    fd.Filters.Add "All Files", "*.*"
    fd.FilterIndex = 1
    var = fd.Show
    If Not var Then
    MsgBox "Please Select a file"
    Exit Sub
    End If
    fd.Execute
    Unload Me


    This code is to save the file

    Dim cnn As ADODB.Connection
    Dim fd As FileDialog
    Dim var As Boolean
    Set cnn = New ADODB.Connection
    cnn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & ThisWorkbook.Path & "\File.accdb;Persist Security Info=False;"
    Set fd = Application.FileDialog(msoFileDialogSaveAs)
    fd.Show
    If Not var Then
    MsgBox "Enter name to save file"
    End If
    fd.Execute
    Unload Me

    But this code is saving my file into selected destination I want is when you run the code it should directly get stored in access table were I have created Attachment field.

  5. #5
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Excel and Access VBA

    Code:
    RS.Fields("REPORT_NUMBERS").Value = "Fieldd"
    Set rsAttachments = RS.Fields("ATTACHMENTS").Value
    rsAttachments.AddNew
    rsAttachments.Fields("FileData").LoadFromFile SelectFile
    rsAttachments.Update
    rsAttachments.Close
    RS.Fields("LOG_TIME").Value = Now
    this is an example using DAO from https://stackoverflow.com/questions/...sing-excel-vba, make sure to read the entire post
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  6. #6

    Thread Starter
    New Member
    Join Date
    Apr 2018
    Posts
    5

    Re: Excel and Access VBA

    Thank you so much for your reply can you please tell me where to insert this code as I am new to this. I really appreciate your help.

  7. #7
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Excel and Access VBA

    where to insert this code
    not really sure as you have 2 procedures above
    are you trying to select a file from a dialog then copy the file into the database? or do you want to store a specific, already known file, or attachment to the database?
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  8. #8

    Thread Starter
    New Member
    Join Date
    Apr 2018
    Posts
    5

    Re: Excel and Access VBA

    I want to store a file from a dialog box and then store it in the database.

    This is the code now I am using but I have problem in declaring object I am doing it by String but it gives me error in INSERT query of Missing operator. So how can I do this. Thank you for help

    Dim fd As FileDialog
    Dim SelectFile As String
    Dim cnn As ADODB.Connection
    Set cnn = New ADODB.Connection
    Dim rst As ADODB.Recordset
    Set rst = New ADODB.Recordset
    Set fd = Application.FileDialog(msoFileDialogOpen)
    With fd
    .AllowMultiSelect = False
    .Title = "Please select file to attach"
    If .Show = True Then
    SelectFile = .SelectedItems(1)
    Else
    Exit Sub
    End If
    End With
    Set fd = Nothing

    cnn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & ThisWorkbook.Path & "\SaveFile.accdb;Persist Security Info=False;"
    cnn.Open
    Set rst = cnn.Execute("INSERT INTO Table1(AddFiles) VALUES('" & SelectFile & "')")
    ' rst.AddNew
    rst.Fields(0).Value = "SelectFile"

    rst.Update
    rst.Close
    cnn.Close

  9. #9
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Excel and Access VBA

    i have never used the attachment field, but in think i saw somewhere that you can not insert into it, you would have to find the correct record (or add a record), then edit it to put the file into the attachment field, with code as i posted
    it appears that an attachment field is like a separate table and can contain multiple files, so you have to add a record to the attachment field, within an existing record or a new record in the parent table

    i did not see any examples using ADO to do this, most responses to this question recommended using DAO loadfromfile
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  10. #10
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    Re: Excel and Access VBA

    Personally I would not recommend doing this as the db file will balloon very quickly (2gb limit remember).
    I'd use a shared area (assuming its a work situation and they have a shared area where the db is) folder and create sub folders to hold the attachments. hold the path to the main folder in the system properties or a global variable and store the path to the attachment in the db...

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width