Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to code to transfe data from excel worksheet to ms access database

Status
Not open for further replies.

hex6007

MIS
Sep 2, 2008
53
0
0
PH
Hi guys,
I want to transfer the data from a column in excel into ms access database. I need help.

thanks in advance

regards
 
nop i havent tried yet. can i have a more specific example with this, please?

thanks.

 

Well, first we may need more specific info...

Are you writing this in VBA, or in VB6, or where?
 
You would use an INSERT INTO (append data to existing table) or SELECT INTO (create new table and append data)sql statement on the connection's .Execute method.

In the code window type the word:
IN
put the cursor on it and hit F1.
Select "VB"
then select "IN clause"
then select "Example"
further down you see an example for Excel.

To append data to an existing table use something like:
Code:
[COLOR=blue]
Public Sub Excel1()
    Dim dbcon As adodb.Connection
    Dim sXlsPath As String
    
    sXlsPath = "C:\MyExcelTable.xls"
    
    Set dbcon = New adodb.Connection
    With dbcon
        .Provider = "Microsoft.Jet.OLEDB.4.0"
        .Properties("Data Source") = "C:\MyDatabase.MDB"
        .Open
    End With
    [/color][COLOR=green]
    '"myTable" is the table in the Mdb
    '"Field1" is a field in "myTable"
    '"A" is the xls column name
    '"Worksheet1$" is the name of the Worksheet[/color][COLOR=blue]
    dbcon.Execute _
    "INSERT INTO myTable(Field1) " & _
    "SELECT [A] AS Field1 " & _
    "From [Worksheet1$] " & _
    "IN '" & sXlsPath & "' 'EXCEL 8.0;'"

    [/color][COLOR=green] 
    'To append data from all columns, and all columns are in myTable with the same names, then you can shorten it as:
     'dbcon.Execute _
    "INSERT INTO myTable " & _
    "SELECT * " & _
    "From [Worksheet1$] " & _
    "IN '" & sXlsPath & "' 'EXCEL 8.0;'"
End Sub[/color]
 
... but be careful, as this does no V&V on the information (data) ...

so may not work or may work and corrupt the information already in your table



MichaelRed


 
MichaelRed
What is V&V ?

If there is a problem with corrupting data in the MS Access db table, then I would think that a recordset could be used instead, and then loop through the records, inspecting the values and then adding them one at a time to the table.

Code:
dim rs as adodb.recordset
dim sCmdTxt as string
Set rs = adodb.recordset

set rs.ActiveConnection = dbcon

sCmdTxt = "SELECT * " & _
    "From [Worksheet1$] " & _
    "IN '" & sXlsPath & "' 'EXCEL 8.0;'"
"
rs.Open sCmdTxt

do until rs.Eof
   'Add records to db table one at a time
loop
 
V&V ~~ Verification and Validation.

and there are numerous approaches to the solution

doing the process on a record by record then field by field basis has, for me, proven painfully slow.

the process I outlined is essentially an attempt to bulk process the imports one feature at a time.

or (verification / validation of 'batches' of information)



MichaelRed


 
To SBerthold and MichaelRed both of your examples i have combined and works perfectly to the result i wanted to. Thanks a lot guys.

Best
 
Hi hex6007
A good way to thank Tek-Tips people who have helped you is to click the link shown on each post, it looks like this:

Thank MichaelRed
for this valuable post!

or

Thank SBerthold
for this valuable post!

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top