If I create a new connection, the first call to SQL, whatever it is, fails.
ConnectionString = CurrentProject.Connection.ConnectionString
Set connection = New ADODB.connection
connection.Open ConnectionString ' Fails here now
End If
If connection.STATE = adStateOpen Then
MsgBox "Success"
Else
MsgBox "Failure to communicate"
End If
connection.Execute "Select 1 from [System Log] where 1 = 0", , adCmdText Or adExecuteNoRecords
Fails with "Object variable or With block variable not set"
If I just use
Set connection = CurrentProject.connection
it works fine.
Connection string now looks like
Provider=Microsoft.ACE.OLEDB.12.0;User ID=Admin;Data Source=C:\Apps\AccessDB.accdb;Mode=Share Deny None;Extended Properties="";Jet OLEDB:System database=C:\Users\user\AppData\Roaming\Microsoft\Access\System.mdw;Jet OLEDB:Registry Path=Software\Microsoft\Office\16.0\Access\Access Connectivity Engine;Jet OLEDB:Database Password="";Jet OLEDB:Engine Type=6;Jet OLEDB:Database Locking Mode=1;Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Global Bulk Transactions=1;Jet OLEDB:New Database Password="";Jet OLEDB:Create System Database=False;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Don't Copy Locale on Compact=False;Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:SFP=False;Jet OLEDB:Support Complex Data=True;Jet OLEDB:Bypass UserInfo Validation=False;Jet OLEDB:Limited DB Caching=False;Jet OLEDB:Bypass ChoiceField Validation=False
Normally, when you create a new connection, you are trying to connect to external data and ADO works great for that.
In this case, I wanted to work through the existing local and linked tables, just with a separate connection so I could have everything in a transaction.
I didn't want to use the existing connection because there are some things that I didn't want to get rolled back on failure, like logging.
In the end, I couldn't get this to work.
I considered moving everything to using DAO to make it easy to access the local data, but that would have been a massive and risky change.
The final version went the other way and switched the logging to using DAO and CurrentDB, while everything else continued to use ADO and CurrentProject.Connection. This way I just set the transaction on the connection and no new ADODB needed.
Related
I recently developed an excel file that uses internal excel connections to update a few of the tabs from a data import tab within the workbook. Everything works great until I email it to one of my co-workers and he tries to update the data. He will get the error message below. I never set a password on the material so I am unsure why this error is occurring.
I cant add a photo (rep isn't high enough yet) but the error text is below.
Error Text
Reenter Password
The Password is missing or invalid for [name of file]. Please verify your Password and reenter it.
Connection string:
Provider=Microsoft.ACE.OLEDB.12.0;User ID=Admin;Data Source=C:\Users\xxxxxx;Mode=Share Deny Write;Extended Properties="HDR=YES;";Jet OLEDB:System database=;Jet OLEDB:Registry Path=;Jet OLEDB:Engine Type=35;Jet OLEDB:Database Locking Mode=0;Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Global Bulk Transactions=1;Jet OLEDB:New Database Password=;Jet OLEDB:Create System Database=False;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Don't Copy Locale on Compact=False;Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:SFP=False;Jet OLEDB:Support Complex Data=False;Jet OLEDB:Bypass UserInfo Validation=False;Jet OLEDB:Limited DB Caching=False;Jet OLEDB:Bypass ChoiceField Validation=False
Just a guess: Since your Data Source points to a filepath, could it be that your co-worker doesnt have the file at the exact same path? Especially since that seems to live under "Users"? Maybe thats just Excel's way of telling you the file is missing?
My spreadsheet is reading data from another spreadsheet using ADO. The VBA code works properly in MS Excel 2007. However, in MS 2013, opening the recordset generates the error: No value given for one or more required parameters.
The line that errors out reads:
recset.Open sqlCommand, _
conn, adOpenStatic, adLockReadOnly, adCmdText
I double checked my two variables.
sqlCommand has the (sanitized) value: SELECT * FROM [Sheet1$] WHERE [F1] IS NOT NULL ORDER BY [F1] ASC.
conn is the Connection object, with (sanitized) value: Provider=Microsoft.ACE.OLEDB.12.0;User ID=Admin;Data Source=C:\Users\user\Documents\path\workbook_to_read.xlsx;Mode=Share Deny None;Jet OLEDB:System database="";Jet OLEDB:Registry Path="";Jet OLEDB:Database Password="";Jet OLEDB:Engine Type=37;Jet OLEDB:Database Locking Mode=0;Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Global Bulk Transactions=1;Jet OLEDB:New Database Password="";Jet OLEDB:Create System Database=False;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Don't Copy Locale on Compact=False;Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:SFP=False;Jet OLEDB:Support Complex Data=False;Jet OLEDB:Bypass UserInfo Validation=False;Jet OLEDB:Limited DB Caching=False;Jet OLEDB:Bypass ChoiceField Validation=False;
The other three parameters are ADO constants.
My best guess is that the Provider is for the wrong version, either of Excel or ADO. I don't know what the correct version is, nor how to make the code work in both environments.
Edit and facepalm:
Most of the Excel files I'm reading from do not have headers for the first two fields. THIS ONE DOES, and I had to replace [F1] with the correct field name. Please close.
Hi i was looking for some answer i came up with this Treating data as text
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\myFolder\myExcel2007file.xlsx;
Extended Properties="Excel 12.0 Xml;HDR=YES;IMEX=1";
And when i run some test in Excel it shows this:
Provider=Microsoft.ACE.OLEDB.12.0 ...
Extended Properties=""HDR=YES;""; ...
Jet OLEDB:Bypass ChoiceField Validation=False;
So i think that your missing parameter must be the Extended Properties
Most of the Excel files I'm reading from do not have headers for the first two fields. THIS ONE DOES, and I had to replace [F1] with the correct field name.
(Answer pasted from my edit on 10 July 2015.)
In an application that uses a MS Access form as a front-end to some SQL databases, I use DBEngine.CreateWorkspace to get a workspace, then workspace.OpenDatabase to connect to my remote SQL server via a defined ODBC System DSN. This all works quite nicely, until someone disconnects the remote SQL machine from the network, or shuts it down, or something else similarly ridiculous. (Note: I know there's a lot ridiculous about this setup, but unfortunately it's an inevitability at this point)
My question is:
Is there a way to elegantly deal with the timeout and subsequent 'SQL Server does not exist or access denied' error messages that come up, within the VBA code? workspace.OpenDatabase throws an error that I can catch and deal with, but not before two popups come up and stop my VBA code until an operator clicks OK.
DoCmd.SetWarnings False doesn't affect it as the error popups are not actually coming from Access itself - I think they're from the underlying ODBC process or the Jet Engine that drives it.
Any ideas?
A good solution can be found here:
ACC2000: How to Trap ODBC Logon Error Messages
http://support.microsoft.com/kb/210319
The above is from Access 2000 and is 14 years old, but as such it still works fine today. The other possible advantage is you don’t have to adopt and introduce ADO into your application. For applications that already use or have ADO, then no big deal, but if your application sticks to one data object model, then you not have to potentially introduce ADO.
The other BIG bonus of the above is this effectively logs you into the database and thus you avoid having user name and passwords in the linked tables. This means you can have different users and logons, and NOT have to re-link or embed the user name or password in your linked tables.
This wonderful trick and result of the above connection trick is outlined here:
Power Tip: Improve the security of database connections
http://blogs.office.com/b/microsoft-access/archive/2011/04/08/power-tip-improve-the-security-of-database-connections.aspx
I eventually found something that works by searching 'Suppress ODBC connection failure warnings'.
Courtesy of Trevor Best from http://bytes.com/topic/access/answers/201502-how-suppress-odbc-connection-dialog
Some code that uses ADO to make the database connection in a way that allows VBA error trapping to catch the error before the system throws any popups at you.
Function CanOpenSQLDbLB(pstrServer As String, pstrDb As String, pstrUser
As String, pstrPassword As String, Optional pfReportError As Boolean =
True) As Boolean
On Error GoTo CanOpenSQLDbLB_Err
Dim objConn As Object
Dim strConn As String
Dim strError As String, lngErr As Long
Const cstrSQLErr = "[Microsoft][ODBC SQL Server Driver][SQL Server]"
Set objConn = CreateObject("ADODB.Connection")
strConn = strConn & "DRIVER=SQL Server"
strConn = strConn & ";SERVER=" & pstrServer
strConn = strConn & ";APP=" & Application.Name
strConn = strConn & ";WSID=AWorkstation"
strConn = strConn & ";DATABASE=" & pstrDb
objConn.Open strConn, pstrUser, pstrPassword
CanOpenSQLDbLB = True
CanOpenSQLDbLB_Exit:
On Error Resume Next
objConn.Close
Set objConn = Nothing
Exit Function
CanOpenSQLDbLB_Err:
lngErr = Err.Number
strError = Err.Description
If InStr(1, strError, cstrSQLErr) Then
strError = "Error reported by server" & vbCr & vbCr &
Replace(strError, cstrSQLErr, "")
End If
Select Case lngErr
Case Else
If pfReportError Then
MsgBox strError, 16, "Error #" & Err & " Attempting to
open server database"
End If
End Select
Resume CanOpenSQLDbLB_Exit
End Function
I'm trying to use VBA from Excel 2007 to execute a query in an Access 2007 DB as a stored procedure. The query retrieves data from a few MS SQL tables linked via an ODBC DSN. The authentication for the external tables is done with Windows NT authentication with Trusted_Connection=Yes
I have tried connecting to the linked tables with both a User and System DSN - either way works fine when I run the query from Access 2007.
When I try using VBA in Excel the data that should be coming from the Windows authenticated tables isn't retrieved. I am able to retrieve data from a set of linked MS SQL tables that are using a stored UID.
Here is the connection string I'm using:
strDB = xlWb.Path & "\database\dbQueries.accdb"
conn.Open "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & strDB & ";"
And here is the command that's only partially successful (with data from the UID authenticated tables):
With cmd2
.ActiveConnection = conn
.CommandText = "qryAppendtblOutput"
.CommandType = adCmdStoredProc
.Parameters.Append cmd2.CreateParameter("[Start Date]", adDBDate, adParamInput, , startDate)
End With
cmd2.Execute
Thanks very much in advance!
I have been able to resolve this issue by switching to the Access ODBC driver instead of the OLE DB provider. I changed my connection string to:
strDB = xlWb.Path & "\database\dbQueries.accdb"
conn.Open "Driver={Microsoft Access Driver (*.mdb, *.accdb)};" & _
"Dbq=" & strDB & ";" & _
"Trusted_Connection=Yes;"
Then, all I had to do was adjust the syntax of the underlying queries in Access to meet the requirements of this driver (I had to replace all double quotes with singles).
Thanks very much to all who reviewed my question :)!
I'm trying to use two different methods of connecting to a database to edit data, the declaration below is working:
Dim rsConn As ADODB.Connection
rsConn.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" _
& Application.StartupPath & "\VFMS_DB.mdb;" _
& "Jet OLEDB:System Database=Security.mdw", "Ads", "1234")
but now I want to do the same thing with the declaration below but I keep getting the message "Not a valid account name or password.
Dim conn As OleDbConnection
conn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" _
& Application.StartupPath & "\VFMS_DB.mdb;" _
& "Jet OLEDB:System Database=Security.mdw;Database Account=Ads;Database Password=1234;")
I pretty sure it's because of this ;Database Account=Ads;Database Password=1234; not being correct. I tried ;User ID=Ads;Database Password=1234; and also to make it look like the first statement but neither worked.
If anyone can please assist.
Thank You
According to connectionstrings.com, this is the right connection string for a Jet OLE DB 4.0 connection with username and password:
Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=C:\mydatabase.mdb;Jet
OLEDB:System Database=system.mdw;User
ID=myUsername;Password=myPassword;
I don't know the actual answer to your problem, but would point out that there is a potential for confusion here. There are two different types of "passwords" with Jet, the database password and the Jet user-level security password. The former involves no username (the database has a single password for everyone), while the latter has username/password pairing.
Jet ULS is always in effect, at all times. The default setup with Jet is a username of "admin" with no password defined. If someone has defined a password for the admin account, you'll need to provide it, or use an account whose password you know.
Last of all, the Jet ULS information is kept in a workgroup file, usually named system.mdw, and you may need to specify that in your connection string if the security settings for the MDB you're attempting to access are stored in a workgroup file other than the one defined as the system's default workgroup file. I'm not certain how ADO/OLEDB interacts with the default workgroup location settings in the registry, so I'd suggest finding out what the parameter name is for it and specifying it explicitly in all cases.
Try using the User ID and Password keywords instead of Database Account and Database Password:
conn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" _
& Application.StartupPath & "\VFMS_DB.mdb;" & _
"Jet OLEDB:System Database=Security.mdw;User ID=Ads;Password=1234;")
This is according to an example that I found on connectionstrings.com.
Have you tried with user id and just password?
The easy way:
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\mydatabase.mdb;Jet OLEDB:Database Password=myPassword