View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
Annelie[_4_] Annelie[_4_] is offline
external usenet poster
 
Posts: 1
Default Export Excel data to Access Security problem

Sub MacroTime()
'replace path with the path and name of your security.mdw'
DBEngine.SystemDB = "c:\DATA\_PCI\ACCESS\security.mdw"
DBEngine.DefaultUser = "YourUserName"

Set ws = DBEngine.Workspaces(0)
Set db = ws.OpenDatabase("YourDatabaseName.mdb")
Set rs = db.OpenRecordset("YourTable", dbOpenTable)

Dim r As Long
' open the database
r = 2 ' the start row in the worksheet assuming there
are headers in excel.
Do While Len(Range("A" & r).Formula) 0
' repeat until first empty cell in column A
With rs
.AddNew ' create a new record
' add values to each field in the record
'Rename to your access field Names '
.Fields("Field1") = Range("A" & r).Value
.Fields("Field2") = Range("B" & r).Value
.Fields("Field3") = Range("C" & r).Value
.Fields("Field4") = Range("D" & r).Value
.Fields("Field5") = Range("E" & r).Value
.Fields("Field6") = Range("F" & r).Value
' add more fields if necessary...
.Update ' stores the new record
End With
r = r + 1 ' next row
Loop
rs.Close
Set rs = Nothing
db.Close
Set db = Nothing
End Sub

Please note that I originally took the code from:
http://www.erlandsendata.no/english/...hp?t=envbamain
which worked fine for an unsecured database. By trial and error I adde
the lines to upen a secured database by taking instructions from
http://support.microsoft.com/default...p#_Toc49329970

--
Message posted from http://www.ExcelForum.com