In the "old" days, in order for me to search a "Table" for specific data in a vba statement in a "Form"/"Event" in MS Access 2003, that I chose. I would use the following code:
Set Table_ID = New ADODB.Recordset
Table_ID.CursorLocation = adUseClient
Table_ID.Open "Select * From Table_ID", _
CurrentProject.Connection, adOpenDynamic, adLockOptimistic
The above code gives me the following message when I ran it MS Access 2019:
Microsoft Visual Basic for Application Compile error: User-defined type not defined
I changed the "ADODB" with the database I'm using in MS Access 2019 as follows:
Set Table_ID = New DB002.Recordset
When I ran it again in MS Access 2019, the same message appeared.
Please advise.
Related
Well this is fascinating. I am using MS-Access 2007 to make a from that will allow users to enter data from lab results.
The from utilizes a VBA code call:
DAO.OpenRecordset("{Sql query}" , DB_OPEN_DYNASET, dbSeeChanges)
This call normally works just fine, but I've noticed that when this form is closed and then re-opened, a 3151 ODBC error is generated.
We are using linked tables through Access 2007 for this form, our SQL-Server 2012 version is as follows.
Microsoft SQL Server Management Studio 11.0.2100.60
Microsoft Analysis Services Client Tools 11.0.2100.60
Microsoft Data Access Components (MDAC) 6.1.7601.17514
Microsoft MSXML 3.0 6.0
Microsoft Internet Explorer 8.0.7601.17514
Microsoft .NET Framework 4.0.30319.237
Operating System 6.1.7601
The DAO.OpenRecordset call is utilized in multiple events. Here is the list.
On_Current , _AfterUpdate , Form_Unload
I have found several reported instances of similar errors on Access 2007 with linked tables to SQL-Server, but none seem to have workable solutions for this issue.
As best I can tell, the 3151 doesn't appear to be affecting the form or data in any negative way. I am able to open the form and everything is where it should be as best I can tell. There are about 20 thousand records on this table, so I may be missing something.
Does anyone have any ideas?
9/14 In response to comments I am adding some additional code.
Here is the call on Form_Unload. It contains the failing line.
Private Sub Form_Unload(Cancel As Integer)
Dim ImportID
Dim rst As DAO.Recordset
Dim db As Database
Set db = CurrentDb
Set ImportID = Me.ImportID
#This is the failing line below.
Set rst = db.OpenRecordset("SELECT [dbo_t_inspect].* FROM [dbo_t_inspect] WHERE [dbo_t_inspect].ImportID= " & ImportID & ";", DB_OPEN_DYNASET, dbSeeChanges)
Here is the call from On_Current. This On_Current call is actually made by a sub-form on the main form.
Private Sub Form_Current()
Dim rst As DAO.Recordset
Dim strImportID As String
If IsNull(Me.ImportID) Then
[Forms]![Inspection Receiving]![LabComplete].Visible = False
[Forms]![Inspection Receiving]![LabPending].Visible = True
Else
strImportID = Me.ImportID
Dim db As Database
Set db = CurrentDb
Set rst = db.OpenRecordset("SELECT [dbo_t_health].*, [dbo_t_health].ImportID FROM [dbo_t_health] WHERE ((([dbo_t_health].ImportID)=" & Me.ImportID & "));", DB_OPEN_DYNASET, dbSeeChanges)
I am not sure where DAO is defined in this solution. I cannot find it's declaration in my folder titled Microsoft Office Access Class Objects I am open to suggestions on where that might be located.
The precise error message is as follows
Failed to connect to {ODBC Connection Name} Error 3151.
I can then select the debug option and the debugger will take me to the line that tried and failed to connect the the ODBC Connection I am using.
The Error is highly reproducible. All I have to do is enable VBA Macros on the Security Alert and then open the form in question. I close out the form after it loads. Once I attempt to reopen the form, I get the error described above. The Form opens anyway after I click Ok on the error.
This codes below are working with visual basic 6.0 and I wanted this code to be used in vb.net and I think there are errors when I typed it on vb.net(Visual Studio 2013)
the name of my MS access database is "mySavings.accdb"
the table name is "Balance" with a field named "Balance"
I already added the reference:
Microsoft ActiveX Data Objects 6.0 Library
Microsoft ActiveX Data Objects Recordset 6.0 Library
Thank you in advance and here's my code in VB6
Public con As New ADODB.Connection
Public rs As New ADODB.Recordset
Dim Amount as String
Private Sub Form_Load()
con.Open ("Provider=Microsoft.ACE.OLEDB.12.0; Data Source = C:\Users\MyPc\Documents\Visual Studio 2013\Projects\mySavings.accdb")
rs.Open ("Select * from Savings"), con, 3, 2
Amount = rs!Balance
msgbox("You're current balance is " & Amount)
End Sub
It's been awhile since I've used ADODB but I think this should work
Amount = rs.Fields.Item("Balance").Value
It's that rs!Balance piece of code that's not right.
Another way to do this:
Dim Amount As Decimal = rs.Fields("Balance").Value
I have an MS Access 2003 database that contains the following query:
SELECT Replace(Trim(TABLE_A.Field_01), "XXX", "YYY") AS FLD01 FROM TABLE_A
If I do an "Import Data" with Excel from this Access database, I can't find the name of this query that is defined in the database.
If I change the query by removing the Trim function, then I can see the query in Excel.
SELECT RTrim(LTrim(TABLE_A.Field_01)) AS FLD01 FROM TABLE_A
Has anyone had a similar experience? I think there's a limitation on what kind of function one can apply to a query in MS Access.
It looks like there is a problem with MS Jet SQL, which doesn't support the Replace() function - searching the key words "Jet Sql Replace Function" in google gives a lot of references with various issues with the same root cause, but I haven't found a decent solution yet...
Trim() function is not a part of SQL (resides in VBA.Strings library) so couldn't be called outside MS Access.
So you can use any SQL function but none of "external".
For what it's worth, the Replace() function is supported by the Access Database Engine 2010 (a.k.a. "ACE", the successor to "Jet"), available here. To verify that I created a table named [SomeTable] in an Access 2003 database file:
ID s
-- ----------------------------
1 Everybody loves tofu!
2 Nobody really liked Raymond.
...and I created a saved query named [stockReplaceQuery]:
SELECT ID, Replace([s],"tofu","bacon") AS s1
FROM SomeTable;
When I tested a Jet ODBC connection using the following VBScript
Option Explicit
Dim con, rst
Set con = CreateObject("ADODB.Connection")
con.Open "Driver={Microsoft Access Driver (*.mdb)};DBQ=C:\Users\Public\2003test.mdb;"
Set rst = CreateObject("ADODB.Recordset")
rst.Open "SELECT s1 FROM stockReplaceQuery WHERE ID = 1", con
WScript.Echo rst(0).Value
rst.Close
Set rst = Nothing
con.Close
Set con = Nothing
...I got
C:\__tmp>cscript /nologo repl.vbs
C:\__tmp\repl.vbs(6, 1) Microsoft OLE DB Provider for ODBC Drivers: [Microsoft][ODBC Microsoft Access Driver] Undefined function 'Replace' in expression.
When I tested an ACE ODBC connection by changing the con.Open line to
con.Open "Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=C:\Users\Public\2003test.mdb;"
...I got
C:\__tmp>cscript /nologo repl.vbs
Everybody loves bacon!
It might be worth a try to install the ACE engine/drivers and see if that helps any.
I wrote a SQL query in MS Access that uses the MonthName function. In access it works flawlessly. I copied the exact SQL statement into an excel module that I frequently use to query databases. When I run the query, excel keeps telling me that MonthName is an undefined function name. If I remove the MonthName portion, the query runs fine.
It seems like I'm missing a reference or something... Right now, I'm referencing Microsoft ActiveX Data Objects Library 6.0. Can anyone point me in the right direction? Thanks
strSQL = "SELECT DISTINCT Customers.CustomerName, Employees.EmployeeName, [Policy data revised].EXDT, MonthName(Month([EXDT])) AS expMonth
FROM (([Service Team table]
INNER JOIN Customers
ON [Service Team table].CustID = Customers.CustID)
INNER JOIN Employees
ON [Service Team table].EmployeeID = Employees.EmployeeID)
INNER JOIN [Policy data revised]
ON Customers.CustID = [Policy data revised].CustID
WHERE ((([Service Team table].RoleExtension)='2. Underwriting Assistant')
AND (([Policy data revised].EXDT)
BETWEEN #" & minExpDt & "# AND #" & maxExpDt & "#))
ORDER BY [Policy data revised].EXDT ASC;"
The MonthName() function is only available for queries run within an Access application session. See the "The following VBA functions won't work when called from a property sheet or used in an SQL statement" bullet point at About Microsoft Jet Expression Service sandbox mode. Within an Access application session, the db engine can use the expression service to use that sandboxed function.
Since you can't use MonthName, try this Format expression instead.
Format([EXDT], 'mmmm') AS expMonth
Strange, I just tried the following code in Excel 2010 (64-bit) and it worked for me:
Sub foo()
'' Reference: "Microsoft ActiveX Data Objects 6.0 Library"
Dim con As ADODB.Connection, rst As ADODB.Recordset
Set con = New ADODB.Connection
con.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Gord\Desktop\Database1.accdb;"
Set rst = New ADODB.Recordset
rst.Open "SELECT MonthName(Month([BookingStart])) FROM Payment_tbl", con, adOpenStatic, adLockOptimistic
Debug.Print rst(0).Value
rst.Close
Set rst = Nothing
con.Close
Set con = Nothing
End Sub
Edit
Further to HansUp's comment, something must have changed between Office 2007 and Office 2010. I ran another test from a 32-bit machine running Office 2010 and the above code run against an Access 2000 .mdb file worked for me using both...
Provider=Microsoft.ACE.OLEDB.12.0;
...and...
Provider=Microsoft.Jet.OLEDB.4.0;
I also checked the SandBoxMode registry value under...
HKLM\Software\Microsoft\Office\14.0\Access Connectivity Engine\Engines
...and it is 3, which is the value for "Enabled" (ref: here).
This Microsoft KB article details how to run a query on another database than the current one used by the Access project. However it only states how to connect to DBase, Foxpro, Paradox, BTrieve and ODBC.
I want to be able to do something like this:
UPDATE MSSQLDatabase.Table
SET MSSQLDatabase.Table.Column = AccessDatabase.Table.Column
WHERE MSSQLDatabase.Table.Column = AccessDatabase.Table.ID
INSERT INTO AccessDatabase.Table
VALUES (AccessDatabase.Table.ID)
Can you give me any pointers of where to begin? The database I want to connect to is a SQL Server 2008 Provider Native connection. I'm using Access 2007.
To do this in VBA would be perfect.
By far the easiest way to work with SQL Server in MS Access is to use linked tables. However, you can also run pass-through queries and refer to a connection in-line:
SELECT * FROM [ODBC;FILEDSN=Z:\Docs\Test.dsn;].table_1
Or
SELECT * FROM
[ODBC;DRIVER=SQL Server;SERVER=srvr;Trusted_Connection=Yes;DATABASE=Test;].table_1
Or
SELECT * FROM [ODBC;Driver={SQL Server Native Client 11.0};Server=svr;Database=test;Trusted_Connection=yes;].table_1
see also http://www.connectionstrings.com/sql-server-2008
This solution allows to catch errors:
Private Sub Command10_Click()
On Error GoTo Err1:
Dim cn As ADODB.Connection
Set cn = New ADODB.Connection
With cn
.Provider = "SQL Native Client"
.ConnectionString = "Server=myserver\myinstance;Database=mydb;Uid=myuser;Pwd=mypass;]"
.Open
End With
MsgBox "Connection successful!"
cn.Close
Exit Sub
Err1:
MsgBox Err.DESCRIPTION
End Sub
The only thing to note, is that within the Visual Basic Editor, you must first go to Tools > References, and check Microsoft ActiveX Data Objects 2.x Library.