Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
|
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 17th, 2006, 07:51 AM
Registered User
 
Join Date: Jul 2006
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default saving table of information back into database

hi,
i'm new to the entire excel vba and database environment and i am trying to set up an Excel macro that saves specific columns of information in Excel back into specific columns of a db2 database using ADO. Im having trouble finding examples online and was wondering if anyone here could give me a simple example of how this is done? Thanks.
 
Old July 17th, 2006, 10:21 AM
Authorized User
 
Join Date: Jun 2006
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Will you be appending to an already existing table in db2? Or creating a new table?

 
Old July 17th, 2006, 01:06 PM
Registered User
 
Join Date: Jul 2006
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

appending to an already existing database

 
Old July 17th, 2006, 01:42 PM
Authorized User
 
Join Date: Jun 2006
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Default

ok. i haven't used ado, i've used dao...so i'll give the code based on that.

Sub Macro1()
   Dim acsObj As Object
   Dim acsTbl As DAO.Recordset
   Dim acsDb As String
   acsDb = "C:\db1.mdb"
   Set acsObj = CreateObject("Access.Application")
   acsObj.Visible = False
   acsObj.OpenCurrentDatabase acsDb
   Set acsTbl = CurrentDb.OpenRecordset("Table1", dbOpenDynaset, dbAppendOnly)
   For irow = 2 To 5
      With acsTbl
         .AddNew
         .Fields("First Name").Value = Worksheets("Sheet1").Cells(irow, 1)
         .Fields("Last Name").Value = Worksheets("Sheet1").Cells(irow, 2)
         .Fields("DOB").Value = Worksheets("Sheet1").Cells(irow, 4)
         .Fields("Income").Value = Worksheets("Sheet1").Cells(irow, 6)
         .Update
      End With
   Next
   acsTbl.Close
   Set acsTbl = Nothing
   acsObj.CloseCurrentDatabase
   acsObj.Quit
   Set acsObj = Nothing
End Sub

note that in the line "acsObj.visible = false", the "false" can be changed to "true" if you want to see access working in front of you. also, remove the lines "acsTbl.Close" and "acsObj.CloseCurrentDatabase" and "acsObj.Quit" if you want to leave the access database open once the appending is done.

code for ADO should be similar, with few syntax changes for the recordset object. other than that, it should be the same.

oh, important! in the vb code window, under tools, and references, you need to check "Microsoft Access Objects" (or something like that), and for the code i just wrote, you also need to check "Microsoft DAO objects"...if you adapt it to ADO, then that's the one you'll need to check.

hope this helps!






Similar Threads
Thread Thread Starter Forum Replies Last Post
Saving Themes Information in SQL 2000 sudhakar_d191080 ASP.NET 2.0 Professional 0 June 8th, 2007 06:54 AM
Geting table information from a control toddw607 Access 2 March 1st, 2007 08:24 AM
Forms not saving information jonwitts Access VBA 2 August 1st, 2005 09:14 AM
Inserting Information From Form into another Table smartgir Access VBA 6 October 22nd, 2004 12:35 AM
updating table information with a form? lanctotd Access 1 June 8th, 2004 12:46 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.