Consulting

Results 1 to 5 of 5

Thread: Link Data to Access DB

  1. #1

    Link Data to Access DB

    Hi,

    I created an .xlt Template. I had it connecting to Access

    Connectivity was lost a month ago. Anyway, during the downtime, a coworker insert some code to save the file as an .xls, which is fine.

    However, we lost the ability to use the template saving a record to a database. At any rate, upon manual file, save, the record now gets saved to an excel file. I changed the template to save to an excel, then import to Access. Also, it gets saved as an individual xls file to a specified location in a folder.

    I don?t have the code at home now, but we can?t make it work both ways simultaneously saving the individual file and as a record in the DB at the same time.

    Is there any way to do both?

    Thank you

  2. #2
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Hi there,

    I'm not sure I follow you 100%, so if I miss the mark, I apologize. If you want to write a new record to an Access table just before you save your workbook, you could try modifying the following to suit your needs:

    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    'Macro purpose: To add record to Marina Access database using ADO and SQL
    Dim cnt As New ADODB.Connection
    Dim rst As New ADODB.Recordset
    Dim strDB As String
    ' Set the string to the path of your database
    strDB = "D:\My Documents\Access Files\Marina.mdb"
    ' Open connection to the database
    cnt.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    "Data Source=" & strDB & ";"
    ' Open recordset based on Moorages table
    rst.Open "INSERT INTO Moorages (CustID,Type,DatePaid,DateStart,DateEnd,Amount)" _
    & " VALUES ('zz-01','Prorate',#1/15/03#,#1/15/04#,#2/28/05#,500)", cnt
    ' Close ADO objects
    cnt.Close
    Set rst = Nothing
    Set cnt = Nothing
    End Sub
    This code would go into the ThisWorkbook module. The hardest part would be modifying the SQL statement to get it to match your table structure, so a breakdown follows:

    "INSERT INTO Moorages (CustID,Type,DatePaid,DateStart,DateEnd,Amount)" _
    & " VALUES ('zz-01','Prorate',#1/15/03#,#1/15/04#,#2/28/05#,500)"
    -Replace "Moorages" with your table's name
    -Replace "CustID,Type,DatePaid, etc..." with the column headings that you want to fill on your table. I believe that you should only need to put in the columns that you actually want to fill with data, providing that any you don't care about can accept NULL values or have default values
    -Change my info in the values section to match what you want. Make sure that you separate each field with , surround text with ', dates with #, and values need no markings

    This can be coded to accept values directly from your worksheet cells as well. More information would be needed to accomplish that though.

    Hope this helps,

    Ken
    Last edited by Aussiebear; 05-04-2023 at 02:01 AM.

  3. #3
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Oops,

    I also forgot to mark one critical component... the above code will error out if a reference is not set to the Microsoft ActiveX Data Objects Library. You can do this by going into the VBE, choosing Tools|References, and scrolling down the list to find it. I don't know what version of Office you're running, but in Office 2003, its the Microsoft ActiveX Data Objects 2.7 Library.

    Cheers,
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  4. #4
    VBAX Regular
    Joined
    May 2004
    Posts
    42
    Location
    I am trying to pass the values from the worksheets cells. I thought that I could just make reference to them by entering the cell that I wanted but I have received a syntax error. How does that work?

  5. #5
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Sorry, my notification email somehow got filed without me reading it...

    Can you post an example (or upload a sample file) of your data? It would help to know where your data resides in the sheet, and what the column headers for your Access tables are.

    If you can give me that info, I can modify it to work for you.

    Cheers,
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





Posting Permissions

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