PDA

View Full Version : VBA: Reference



ckmi1
03-22-2019, 02:07 PM
I attempting to save data from an Excel Sheet to an Access Table. The number one problem is I'm not an administrator on my system. When I attempt to set the references to the data objects in references I get the message that I'm unable to access the system registery.
My question is there way to add those references that will work on all systems.
If there isn't a way to add those references. Help !!.
This code works until "AddNew" and I receive a run-time error 3251.
Code: (Note: This code works on a system admin rights)


Sub test()
' Database information
DBFullName = Sheet6.Range("T2")
' Open the connection
Set Connection = CreateObject("adodb.Connection")
Cnct = "Provider=Microsoft.Jet.OLEDB.4.0; "
Cnct = Cnct & "Data Source=" & DBFullName & ";"
Connection.Open ConnectionString:=Cnct
' Create RecordSet
Set Recordset = CreateObject("adodb.Recordset")
' Next two lines critical to work in QPro properly. Excel does not need them.
Recordset.CursorType = adOpenKeyset
With Recordset
' Filter
Src = "SELECT * FROM VisitData"
'Src = Src & "and CategoryID = 30"
.Open Source:=Src, ActiveConnection:=Connection
' Cells.Clear 'Used in Excel to clear a sheet
' Write the field names
'For Col = 0 To .Fields.Count - 1
'Range("A1").Offset(0, Col).Value = Recordset.Fields(Col).Name 'Excel method
'Next
.AddNew
Recordset(Sheet6.Range("D2")) = Sheet1.Range(Sheet6.Range("B2"))
Recordset(Sheet6.Range("D3")) = Sheet1.Range(Sheet6.Range("B3"))
Recordset(Sheet6.Range("D5")) = Sheet1.Range(Sheet6.Range("B5"))
.Update
End With
Set Recordset = Nothing
Connection.Close
Set Connection = Nothing
End Sub

Kenneth Hobs
03-22-2019, 06:42 PM
Welcome to the forum! Please paste code between code tags. Click the # icon on reply toolbar to insert the tags.

Is DBFullname a drive:\path\filename.dbf or a DSN? If the later, it has to be set up by and administrator. Press the Windows key and type ODBC and select 32 or 64 bit as used by the database.

Adding the DSN will add entries to the registry as would installation of MDAC. I am not sure which you are talking about for the registry issue.

Obviously, your connection string must be right. https://www.connectionstrings.com/

There may be a need for username or password?

What does a Compile show?