Exporting Excel cells to Access using VBA

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ineedhelpplz
    New Member
    • Dec 2013
    • 19

    Exporting Excel cells to Access using VBA

    The code below works but I don’t understand the Values (line line 9). I keep getting error messages. Could someone add more fields to it, such as:
    F24 into Ingredient2
    F25 into Ingredient3
    F26 into Ingredient4
    G24 into Lot2
    G25 into Lot3
    G26 into Lot4
    The Ingredient column and the Lot column will both have 30 rows for data.

    Code:
    Sub Button10_Click()
    Dim strSQL As String
     
    Set appAccess = CreateObject("Access.Application")
     
    appAccess.Visible = False
     
    strSQL = "INSERT INTO [Finished Batches] ([Production Date],[Lot_Number],[Ingredient1],[Amount1]) " & _
             "VALUES (#" & Range("C5") & "#,'" & Range("D5") & "','" & Range("F23") & _
             "'," & Range("G23") & ")"
     
    With appAccess
      .OpenCurrentDatabase ("C:\users\jay neuh\desktop\database\ss database.mdb")
      .DoCmd.RunSQL strSQL, dbFailOnError
      .CloseCurrentDatabase
    End With
     
    Set appAccess = Nothing
    End Sub
    Last edited by Ineedhelpplz; Jan 28 '15, 02:15 PM. Reason: added detail
  • twinnyfo
    Recognized Expert Moderator Specialist
    • Nov 2011
    • 3662

    #2
    If you already have the Access Project, it might be easier to import into Access from Access. Access allows you to link Spreadsheets and treat them as tables. This greatly speeds up the import process.

    Comment

    • Ineedhelpplz
      New Member
      • Dec 2013
      • 19

      #3
      I wish I could attach my spreadsheet, I don't think it will work as a table. It contains a bunch of calculations.

      Comment

      • Ineedhelpplz
        New Member
        • Dec 2013
        • 19

        #4
        Here is the spreadsheet I'm using.

        MEGA provides free cloud storage with convenient and powerful always-on privacy. Claim your free 50GB now!
        Last edited by zmbd; Feb 2 '15, 08:02 PM. Reason: [z{please keep in mind, most of us cannot access mass-storage sites as we work in secure environments :) }]

        Comment

        • twinnyfo
          Recognized Expert Moderator Specialist
          • Nov 2011
          • 3662

          #5
          I cannot open the link.

          Do you need the calculations or only the values represented by the calculations?

          Comment

          • Ineedhelpplz
            New Member
            • Dec 2013
            • 19

            #6
            I need the values. I hope this link will work, I think it would really help it you saw me excel sheet. Maybe this would be easier if I copied ( =sheet1!a1 ) all the values to sheet2 in one long row with a header row.
            MEGA provides free cloud storage with convenient and powerful always-on privacy. Claim your free 50GB now!

            Comment

            • twinnyfo
              Recognized Expert Moderator Specialist
              • Nov 2011
              • 3662

              #7
              If you only need the values, then you can still link the Spreadsheet, as only the value is displayed, not the calculation or formula.

              Comment

              • Ineedhelpplz
                New Member
                • Dec 2013
                • 19

                #8
                Thanks twinnyfo, Im willing to try it. I want the user to be able to use the info on the spreadsheet and enter more data onto it, then export into access. Each time the sheet is used it will have a new name. I would still like to see what Lines 8,9 and 10 would look like if the select and insert were changed to:

                F24 into Ingredient2
                F25 into Ingredient3
                F26 into Ingredient4
                G24 into Lot2 (Formated as number
                G25 into Lot3 (Formated as number
                G26 into Lot4 (Formated as number

                Comment

                • twinnyfo
                  Recognized Expert Moderator Specialist
                  • Nov 2011
                  • 3662

                  #9
                  If you simply want to expand the code you have above, all your "Insert Into" values are on the same line and all your "Values" are on another line. Gradually expand your code to make sure it works with each addition and you should be able to work it through.

                  We can help with trouble shooting.

                  It looks like your spreadsheet is less of a "table" and just holds somewhat random, but standardized information.

                  Comment

                  Working...