Insert into Access Select from Excel from within Access VBA

核能气质少年 提交于 2019-12-04 12:59:05
HansUp

You don't need to create a New ADODB.Connection which connects to the database you currently have open in your Access session. Your Con object variable can simply reference CurrentProject.Connection ...

Set Con = CurrentProject.Connection

However, you don't really even need that object variable. Just use the Execute method directly from CurrentProject.Connection ...

CurrentProject.Connection.Execute SQLString

That is simpler than what you have now, but I'm not certain it will eliminate the "state ... that prevents it from being opened or locked" complaint.

Since you said "alternate suggestions are welcome", consider DAO instead of ADO for this. And do not include parentheses around the field expression list in the SELECT piece of your INSERT query (regardless of whether you choose ADO or DAO).

Private Sub TransferData(ByVal Company As String, ByVal Address As String, ByVal XLName As String)
    Dim SQLString As String

    SQLString = "INSERT INTO SatSurvey " & _
                "(ClinicID, Method) " & _
                "SELECT '<location>', IDFormat " & _
                " FROM [Excel 12.0;HDR=Yes;Database=" & XLName & "]." & Address
    Debug.Print SQLString
    CurrentDb.Execute SQLString, dbFailOnError
End Sub
ASH

You will find several ways to move data from Excel to SQL Server in the links below.

http://www.excel-sql-server.com/excel-import-to-sql-server-using-distributed-queries.htm

http://www.excel-sql-server.com/excel-sql-server-import-export-using-vba.htm#Excel%20Data%20Export%20to%20SQL%20Server%20Test%20Code

Obviously, you run those from Excel. Also, consider looping through files using SQL Server (opposite from what is described above).

DECLARE @intFlag INT
SET @intFlag = 1
WHILE (@intFlag <=10000)
BEGIN

PRINT @intFlag


declare @fullpath1 varchar(1000)
select @fullpath1 = '''\\FTP\your_path' + convert(varchar, getdate()- @intFlag , 112) + '_your_file.txt'''
declare @cmd1 nvarchar(1000)
select @cmd1 = 'bulk insert [dbo].[your_table] from ' + @fullpath1 + ' with (FIELDTERMINATOR = ''\t'', FIRSTROW = 2, ROWTERMINATOR=''0x0a'')'
exec (@cmd1)


SET @intFlag = @intFlag + 1

END
GO

This script assumes your files have a date in the name of the file. Hopefully your Excel files have some kind of pattern in the name of the file, and you can leverage that.

I find it is easier to do (and debug!) this by actually linking or importing the table from Excel. Then you can use it like any other table.

DoCmd.TransferSpreadsheet acLink, acSpreadsheetTypeExcel12Xml, "TempImport", XLName, True
' If the range is needed, linking doesn't work. Use import instead.
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12Xml, "TempImport", XLName, True, Address

SQLString = "INSERT INTO SatSurvey ... SELECT ... FROM TempImport"
CurrentDb.Execute SQLString

' Clean up temp table
DoCmd.DeleteObject acTable, "TempImport"
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!