INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

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

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

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

(OP)
Hi guys,
I want to transfer the data from a column in excel into ms access database. I need help.

thanks in advance

regards
 

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

Have you considered DoCmd.TransferSpreadsheet from the Access end?

http://lessthandot.com

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

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

thanks.

 

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


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

Are you writing this in VBA, or in VB6, or where?

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

(OP)
im writing in vb6.

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

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


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
    

    '"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

    dbcon.Execute _
    "INSERT INTO myTable(Field1) " & _
    "SELECT [A] AS Field1 " & _
    "From [Worksheet1$] " & _
    "IN '" & sXlsPath & "' 'EXCEL 8.0;'"

    

    '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
 

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

... 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


 

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

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

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

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


 

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

(OP)
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

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

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!

http://lessthandot.com

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close