Import Excel to Access using Excel VBA

Rafaeljunio.ti

New Member
Joined
Dec 15, 2011
Messages
5
Good morning. I need a help, I'm creating an application in Excel VBA using the database access, I'm not experienced with VBA then I am facing many difficulties.

Well what I need is to create an import form where I can import the excel spreadsheet to a new table in access ... but this code should add a field in table called "DATA" with the current date in all rows.

I was trying to do using the code below.

Code:
Private Sub btn_importar_Click ()
Dim cn As New ADODB.Connection
     Dim rs As New ADODB.Recordset
    
     Set cn = New ADODB.Connection
     cn.ConnectionString = "Provider = Microsoft.Jet.OLEDB.4.0; Data Source = H: \ Material for testing \ teste.mdb;"
     cn.CursorLocation = adUseClient
     cn.Open
     If Me.Txtcaminho <> "" Then

'Error Handling If the table does not exist
On Error Resume Next
'Delete the old table before importing the new
DoCmd.DeleteObject acTable, "verification"
On Error GoTo 0

'It is the table

DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12, "verification", Txtcaminho, True
CurrentDb.Execute ("ALTER TABLE ADD COLUMN date Text Verification;")
Dim db As Database, rst As Recordset

Set db = CurrentDb ()
Set rst = db.OpenRecordset ("SELECT * from checking;")

rst.MoveFirst

Do While Not rst.EOF
rst.Edit
'Name the field "date" Date equal to, if you want Date / Time Date By Now replace ()
rst.Fields ("date") = Date

rst.Update
rst.MoveNext
loop

Set rst = Nothing
Set db = Nothing
MsgBox "Table imported successfully", vbInformation
else
MsgBox "Select a spreadsheet", vbInformation
end If
end Sub
end Sub
 
Last edited by a moderator:

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off

Forum statistics

Threads
1,223,236
Messages
6,170,912
Members
452,366
Latest member
TePunaBloke

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top