Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / Importing / Linking / August 2004

Tip: Looking for answers? Try searching our database.

Using VBA to Import from Excel to Access

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Wheat - 03 Aug 2004 19:19 GMT
All,

I'm currently building a database to store information
regarding quality checks.  My QA dept uses an excel file
to "grade" work.  Once a grade has been completed, qa
clicks a button to have the information stored on a server
for viewing by the gradee.

I would like to use a vba function to import the required
data from Excel to Access rather than manually importing
the data for each quality check (up to 100+ per day).

I'm using the following code to insert the information I
need:

Public Function fAddRecordToAccess()

Dim ws As Workspace
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim strSQL As String
Dim intBPID As Integer
Dim strAcct As String
Dim strCSR As String
Dim intQA As Integer
Dim intScore As Integer
Dim dtmDate As Date

intBPID = Sheet1.Cells(1, 4).Value
strAcct = Sheet1.Cells(4, 4).Value
strCSR = Sheet1.Cells(4, 6).Value
intQA = Sheet1.Cells(6, 12).Value
intScore = Sheet1.Cells(2, 12).Value
dtmDate = Sheet1.Cells(4, 11).Value

Set ws = DBEngine.Workspaces(0)
Set db = ws.OpenDatabase("S:\Training Team\Training
Analyst\Testing\Import Test.mdb", , False, True)
Set rs = db.OpenRecordset("tblTest", dbOpenDynaset)

strSQL = "INSERT INTO tblTest Values
(intBPID,strAcct,strCSR,intQA,intScore,dtmDate)"

Set ws = Nothing
Set db = Nothing
Set rs = Nothing

End Function

I'm not sure if I'm even heading in the right direction or
if I'm totally off since this is all new to me.  As of
right now, this code does nothing.  Any help is
appreciated.

Thanks in advance
John Nurick - 04 Aug 2004 21:22 GMT
Hi Wheat,

Comments inline.

>All,
>
[quoted text clipped - 34 lines]
>Set db = ws.OpenDatabase("S:\Training Team\Training
>Analyst\Testing\Import Test.mdb", , False, True)

>Set rs = db.OpenRecordset("tblTest", dbOpenDynaset)
You don't need the recordset.

>strSQL = "INSERT INTO tblTest Values
>(intBPID,strAcct,strCSR,intQA,intScore,dtmDate)"

VBA doesn't interpolate (substitute) variables into strings like that.
You have to concatenate them explicitly and enclose string values in
single quotes (double quotes are optional)

 strSQL = "INSERT INTO tblTest VALUES (" _
   & intBPID & ",'" & strAcct & "','" _
   & strCSR & "'," & intQA & "," & intScore _
   & ",#" & Format(dtmDate, "mm/dd/yyyy") _
   & "#);"

Then execute the query:

 db.Execute strSQL, dbFailOnError

and finally tidy up

 db.Close
 
>Set ws = Nothing
>Set db = Nothing
[quoted text clipped - 8 lines]
>
>Thanks in advance

--
John Nurick [Microsoft Access MVP]

Please respond in the newgroup and not by email.
Wheat - 05 Aug 2004 14:48 GMT
John,

Thank you for your help!

>-----Original Message-----
>Hi Wheat,
[quoted text clipped - 82 lines]
>Please respond in the newgroup and not by email.
>.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.