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