Transfering data between External SQL and MS Database - using VBA in EXCEL



Hey:

I have to loop through 150 + MS Acces databases to combine them into one
SQL Server table. I am using VBA Code to find and import each table. There
is only one table within each database that I need to obtain.
Hope this purpose suffices.
My usual sql statements won't work. I made one that works some of the time
but then runs into a problem when it finds a " ' " in one of the text
fields(7).
I am working an if statement to catch it and take it out but I am not
supposed to change the data. If I input the data through the SQL Server
database it comes in just fine. Again I do not want to import 150 plus
tables manually. My code is below:

strAccessDBConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & strFilePath & strFileName & ";" & _
"User ID=;" & _
"Password=;"

Set cnnMSA = New ADODB.Connection
cnnMSA.Open strAccessDBConnection
Set rstMSA = New ADODB.Recordset
rstMSA.ActiveConnection = cnnMSA.ConnectionString
rstMSA.Source = (Left(strFileName, 6) & "_AUG04")
rstMSA.Open Options:=adCmdTableDirect

strSQLDBConnection = "Provider=sqloledb;Integrated Security=SSPI;Persist
Security Info=False;" & _
"Data Source=CLTL-663P351;" & _
"Initial Catalog=Diagnostic AR;"

Set cnnSQL = New ADODB.Connection
cnnSQL.Open strSQLDBConnection
Set rstSQL = New ADODB.Recordset
rstSQL.ActiveConnection = cnnSQL.ConnectionString
rstSQL.Source = ("tbl_UPTO_AUG04_HS_DATA")
rstSQL.Open Options:=adCmdTableDirect

Do Until rstMSA.EOF
**** This is what I would like to use but it will not find the
rstMSA.Source within the rstMSA.EOF?
*******************************************************************
*** commented out until the next row of "*************"
'strSQL = "INSERT INTO [" + rstSQL.Source + "]"
'+ " SELECT accountnumber AS accountnumber, transactiondate AS
transactiondate," _
'+ " financialclass AS financialclass, facilityid AS facilityid," _
'+ " visitnumber AS visitnumber, dos AS dos, facilityname AS facilityname,"
_
'+ " insname AS insname, revenue AS revenue, payment AS payment," _
'+ " adjustment AS adjustment, dosMonth AS dosMonth, dosYear AS dosYear," _
'+ " transMonth AS transMonth, transYear AS transYear, transmoyr AS
transmoyr," _
'+ " dosmoyr AS dosmoyr, facilitystate AS facilitystate" _
'+ " FROM ( Select * from " + rstMSA.Source + ";)"

****** This works until I until field 7 see a "'" in the data
*****************************************
strSQL = "INSERT INTO [" + rstSQL.Source + "]" _
+ " Values ('" & rstMSA.Fields(0) & "', '" & rstMSA.Fields(1) & "', " _
+ "'" & rstMSA.Fields(2) & "', '" & rstMSA.Fields(3) & "', " _
+ "'" & rstMSA.Fields(4) & "', '" & rstMSA.Fields(5) & "', " _
+ "'" & rstMSA.Fields(6) & "', '" & rstMSA.Fields(7) & "', " _
+ "Cast('" & rstMSA.Fields(8) & "' as money), Cast('" & rstMSA.Fields(9) &
"' as money), " _
+ "Cast('" & rstMSA.Fields(10) & "' as money), '" & rstMSA.Fields(11) & "',
" _
+ "'" & rstMSA.Fields(12) & "', '" & rstMSA.Fields(13) & "', " _
+ "'" & rstMSA.Fields(14) & "', '" & rstMSA.Fields(15) & "', " _
+ "'" & rstMSA.Fields(16) & "', '" & rstMSA.Fields(17) & "')"

cnnSQL.Execute strSQL
rstMSA.MoveNext
Loop


C. Pete Straman
pete.straman@xxxxxx - please feel free to forward direct.

--
Message posted via http://www.accessmonster.com
.



Relevant Pages

  • Re: ADO Recordset Update & SQL Server 2005 ??
    ... I've tried explicit .Update inside the loop and outside the loop as a batch ... There's no 500 error (asp and iis 6) posted on the page. ... This error shows up ONLY when connected to a sql server 2005 server. ... the same database schema/data in sql server 2005, ...
    (microsoft.public.data.ado)
  • RE: adding record via form
    ... the new table 'B' already existed in the database. ... > the information in the three text boxes and add them to table 'B'. ... >> programmatically creating the table on the SQL server via Access? ... put this VBA code in the form's open event: ...
    (microsoft.public.access.forms)
  • Re: How do you store .JPG in ACCESS using VB.NET
    ... that connects to SQL Server directly. ... You may find some helpful VBA code and information at Stephen Lebans' Web ... See http://www.QBuilt.com for all your database needs. ...
    (microsoft.public.access.modulesdaovba)
  • Re: Update 300 DBs with .sql file
    ... Senior Database Administrator ... I support the Professional Association for SQL Server ... > I used to enclose everything into a long string, then loop through each ... > database, use that database, and execute the long string statement. ...
    (microsoft.public.sqlserver.server)
  • Re: ADO Recordset Update & SQL Server 2005 ??
    ... I have a loop: ... rs.activeconnection = nothing 'disconnect the conn ... the IIS box and SQL Server box are separated. ... the same database schema/data in sql server 2005, ...
    (microsoft.public.data.ado)

Loading