Failed VBA SQL Query in Outlook 365? - mysql

I have some VBA code in Outlook that runs a SQL query and populates a userform I designed. This code was previously running fine on one PC, then I was issued a new laptop and I copied the outlook VBA module directly over.
When I try to run, I just get an error "Compile Error: Can't find project or library" and the text "adOpenStatic" in the RS.open statement is highlighted. Previously, I had seen the same error if my SQL query was bad or the driver was missing, but I double checked both. I copied the EXACT code into an excel VBA module and it ran as expected.
The only other difference I can see is that I went from Outlook 2019 to Office 365 for Enterprise.
Dim server, database, login_user, password, port As String
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim strSQL As String
server = "dummy"
database = "dummy"
login_user = "dummy"
password = "dummy"
port = "dummy"
Set cn = New ADODB.Connection
cn.ConnectionString = "DRIVER={MySQL ODBC 8.0 ANSI Driver};Provider=MSDASQL;" & "SERVER=" & server & ";" & " DATABASE=" & database & ";" & "UID=" & login_user & ";PWD=" & password & "; OPTION=3; PORT=" & port & ";Connect Timeout=20;"
'open the connection
cn.Open
Set rs = New ADODB.Recordset
strSQL = "SELECT * FROM quote WHERE id = 1505"
rs.Open strSQL, cn, adOpenStatic
With rs
Do Until .EOF
'parse out relevant project information
rs.MoveNext
Loop
End With
rs.Close
cn.Close
Set rs = Nothing
Set cn = Nothing
I can run the exact query in MySQL workbench, and it works as expected. The fact that the identical code can run in Excel without issue tells me there is perhaps a syntax difference in Outlook that I am unaware of? Anyone else have ideas?
NOTE, if it wasn't already obvious, I am omitting my database details with "Dummy", but the real code has the correct strings.

This code was previously running fine on one PC, then I was issued a new laptop and I copied the outlook VBA module directly over.
You need to re-add all COM references you had on the old machine.
From the Tools menu, choose References to display the References dialog box.
The References dialog box shows all object libraries registered with the operating system. Scroll through the list for the application whose object library you want to reference. If the application isn't listed, you can use the Browse button to search for object libraries (*.olb and .tlb) or executable files (.exe and *.dll on Windows). References whose check boxes are selected are used by your project; those that aren't selected are not used, but can be added.

Go to the VBA Editor, and choose Tools-References; and tick the box next to 'Microsoft ActiveX Data Objects' (the latest version). It should compile ok now.

Related

Querying excel file through ADO Access connection, where are tables stored?

Although I have queried Excel worksheets using an Access ADO connection before, I only thought of the ADO connection as abstract - that is, Access wasn't actually creating a table with my worksheet, and everything "vanished" once I closed the connection.
My thinking changed when I tried explicitly creating a unique table for the first time, and even after closing the connection and exiting the sub, the table still existed somewhere. Where in my directory is this table stored and aside from dropping the table through another VBA connection is there any other way to view/delete it?
You can see what I mean if you run this sub twice. The first time it works fine, and the 2nd time a message comes up saying the table already exists.
Sub stquestion()
Dim acctcon As New ADODB.Connection
Dim acctrec As New ADODB.Recordset
With acctcon
.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\name\Documents\Book1.xlsx;" & _
"Extended Properties=" & Chr(34) & "Excel 12.0 Xml;HDR=YES" & Chr(34) & ";"
querystr = "CREATE TABLE [tbl1] (cost Numeric(10));"
.Open
.Execute querystr
.Close
End With
End Sub
When you open an ADO connection to an Excel workbook and then execute a CREATE TABLE statement, the "table" you create is a new worksheet.
Open Book1.xlsx to see the worksheet created by your code.
When I ran your code with Access 2010 and my workbook, the new sheet was named _tbl. I don't understand why the name was prefixed with an underscore. If that's an issue, you could rename the sheet afterward.

Is it possible to use VBA to change the Current accdb/e Database password

I have a personal DB app that was initially designed using the mdb format in Access 2007. For security reasons I've converted it to .accdb. All functions converted fine except the change DB password function. This function is done in VBA because the Db has all the tool bars turned off. In mdb format... this works fine
DBPath = [CurrentProject].[FullName]
' Create connection string by using current password.
strOpenPwd = ";pwd=" & OldPswd
' Open database for exclusive access by using current password. To get
' exclusive access, you must set the Options argument to True.
Set dbsDB = OpenDatabase(Name:=DBPath, _
Options:=True, _
ReadOnly:=False, _
Connect:=strOpenPwd)
' Set or change password.
With dbsDB
.NewPassword OldPswd, Pswd2
.Close
End With
Me.DB_Pswd = Pswd2
Set dbsDB = Nothing
I found something from this very forum that comes close for the .accdb but it only works for another .accdb file not the current project....
strAlterPassword = "ALTER DATABASE PASSWORD [" & NwPswd& "] [" & OldPswd & "];"
Set ADO_Cnnct = New adodb.Connection
With ADO_Cnnct
.Mode = adModeShareExclusive
.Provider = "Microsoft.ACE.OLEDB.12.0"
' Use old password to establish connection
.Properties("Jet OLEDB:Database Password") = OldPswd
'name current DB
DBPath = [CurrentProject].[FullName] <- this does not work: get a file already in use error
.Open "Data Source= " & DBPath & ";"
' Execute the SQL statement to change the password.
.Execute (strAlterPassword)
End With
'Clean up objects.
ADO_Cnnct.Close
Set ADO_Cnnct = Nothing
So is there a way to do this in VBA for .accdb files? Basically it would be automating the tool bar function of first Decrypt and the encrypt with a new password. I know the tool bar can do so I know there must be a VBA way to do it.
I found the fix for this or maybe just a work around. By removing the ADO library the first method will work for .Accde format files. It will not work for the .accdb format file, but you don't want to distribute those anyway.

Adding linked table to Access 2003 while keeping ODBC connection info in MDB

I have an Access 2003 database MDB where all of the tables exist as linked tables within SQL Server 2005. The MDB file contains all of the ODBC information that points to the correct SQL Server and log-on credentials (trusted connection).
What I would like to do is add a new linked table to the MDB file however I am not sure how to go about specifying the ODBC connection information. When I try to add a new linked table I keep getting prompted to locate or create a DSN file. I don't want to have to create a new DSN entry on every machine, rather I would like all that information stored within the Access MDB file itself.
In the existing database I can "hover" over the table names and see the ODBC connection info as a tool-tip. All I need to do is add another linked table using the same connection information.
I do have access to the SQL Server where the tables are linked to,. I have already created the new table I wanted to add. I just need to find a way to link to it.
For what it's worth, I'm lazy. I keep a DSN on my development machine, and use it to create new linked tables. I then run Doug Steele's code to convert the links to dsnless connections before distribution of the front end to the end users.
You can use the connection string from an existing table, or you can do something like:
''This is a basic connection string, you may need to consider password and so forth
cn = "ODBC;DSN=TheDSNName;Trusted_Connection=Yes;APP=Microsoft Office 2010;DATABASE=TheDatabaseName;"
There are a few was to connect a table:
sLocalName = "TABLE_SCHEMA" & "_" & "TABLE_NAME"
With CurrentDb
If DLookup("Name", "MSysObjects", "Name='" & sLocalName & "'") <> vbNullString Then
If .TableDefs(sLocalName).Connect <> cn Then
.TableDefs(sLocalName).Connect = cn
.TableDefs(sLocalName).RefreshLink
End If
Else
''If the table does not have a unique index, you will neded to create one
''if you wish to update.
Set tdf = .CreateTableDef(sLocalName)
tdf.Connect = cn
tdf.SourceTableName = "TABLE_NAME"
.TableDefs.Append tdf
.TableDefs.Refresh
End If
End With
This will produce a message box if the table does not have a unique index
DoCmd.TransferDatabase acLink, "ODBC Database", cn, acTable, "TABLE_NAME", sLocalName
I was able to add the table successfully and wanted to detail the steps here.
I added the new table by clicking "New" within Access and chose "Link Table"
When prompted with the Link file dialog I chose ODBC from the file type list box
I created a new DSN item (only used for initial linking to table)
Proceed with creating DSN and you will follow process of linking to the table created in SQL Server
Table should show up in Access
I then created the following sub routine in the code module. It essentially loops through all your tables in Access that have ODBC connections and sets the proper ODBC connection info into the Table definitions. You can delete the DSN you created previously as it is no longer needed.
Public Sub RefreshODBCLinks()
Dim connString As String
connString = "ODBC;DRIVER=SQL Server Native Client 10.0;" & _
"SERVER=<SQL SERVER NAME>;UID=<USER NAME>;" & _
"Trusted_Connection=Yes;" & _
"APP=<APP NAME>;DATABASE=<DATABASE NAME>"
Dim db As DAO.Database
Dim tb As DAO.TableDef
Set db = CurrentDb
For Each tb In db.TableDefs
If Left(tb.Connect, 4) = "ODBC" Then
tb.Connect = connString
tb.RefreshLink
Debug.Print "Refreshed ODBC table " & tb.Name
End If
Next tb
Set db = Nothing
End Sub
NOTE ... To execute the above SubRoutine I just typed in it's name into the "immediate" windows within the Access code module. You could also create a macro that executes this routine and then whenever you create a new table you could just run the macro.
Thanks to "Remou" for his answer and assistance!
P.S. If you are interested in APP=<APP NAME> in the connection string and what it is for check out this article. http://johnnycoder.com/blog/2006/10/24/take-advantage-of-application-name/

ADODB Connection String: Workgroup Information file is Missing?

I have a few data sources in access that I need to connect to programatically to do things with behind the scenes and keep visibility away from users.
Said datasource has a password 'pass' as I'm going to call it here. Using this connection method I get an error attempting to use the open method
Dim conn as ADODB.Connection
Set ROBBERS.conn = New ADODB.Connection
conn.open "Provider=Microsoft.Jet.OLEDB.4.0;" _
& "Data Source=\\pep-home\projects\billing\autobilling\DPBilling2.mdb;" _
& "Jet OLEDB:Database Password=pass;", "admin", "pass"
"Cannot start your application. The workgroup information file is missing or opened exclusively by another user."
Due to planning to move into 2007, we are not using nor have ever used a workgroup identification file through access. The database password on the data source was set through the Set Databa Password which had to be done on an exclusive open.
Ive spent a good while changing around my connection options, where to put the passwords etc and either cannot find the right format, or (why I'm asking here) I think there may be some other unknown that I must setup to do this. Anyone out there got some useful information?
Your connection string seems to be incorrect. Try:
conn.open "Provider=Microsoft.Jet.OLEDB.4.0;" _
& "Data Source=\\pep-home\projects\billing\autobilling\DPBilling2.mdb;" _
& "Jet OLEDB:Database Password=MyDbPassword;"
-- http://www.connectionstrings.com/access

Access 2003 via Citrix: 'Error Loading DLL' with CurrentProject.Connection

Set up an Access Project to be opened via Citrix. However, there is some VBA code that prevents it from working:
Dim rs As ADODB.Recordset
Set rs = CurrentProject.Connection.Execute("Query")
The .Connection bit is highlighted. This works when it is not opened via Citrix though (i.e. just on users desktop).
Problem was due to the Citrix server not having the latest Jet Service Pack for Access.
I don't have a lot of experience with ADO but I'm thinking there's a references problem of some sort. Or that you are referencing a newer version of ADO on your system than is avaialble on the Citrix box. Run the following code and post back the results.
Sub ViewReferenceDetails()
Dim ref As Reference
For Each ref In Access.References
Debug.Print ref.Name & " - " & ref.Major & "." & ref.Minor & " - " & ref.FullPath
Next ref
End Sub
Also when you state Access project do you mean an ADP against SQL Server or an MDB/ACCDB against an Access data file?
Your code looks wrong to me. Should the rs Object not be a Recordset?
i.e.
Dim rs As ADODB.Recordset
'Instead of
Dim rs As ADODB.Connection
The code should not run at all - you should receive a Type Mismatch error.