How to fix runtime error '3048' "Cannot open any more databases." - sql

I have a form with a button that calls and filters a couple of union queries with about 40 SELECT queries total in between them. It then displays the data in a report. Each SELECT query in the Union query collects records from multiple unique tables in the database. I recently had to add a couple more SELECT queries into the union query to grab records from new tables which is when I got the runtime error. It was opening the report fine before I added these SELECT queries so im under the assumption is there are too many SELECT queries in the UNION query. To resolve this issue, do I simply not use a UNION query and find an alternative way to combine records? or is it something in the VBA code that needs adjustment?
Here is my code
Private Sub Command189_Click()
DoCmd.SetWarnings False
DoCmd.Close acReport, "Operator Daily Review"
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim varItem As Variant
Dim strCriteria As String
Dim strSQL As String
Set db = CurrentDb()
Set qdf = db.QueryDefs("Productivity_WeeklyFinal")
Set qdf2 = db.QueryDefs("qFiller_Names")
strSQL = "SELECT Info_ME_Employees.ID, gs_1_week_finalUnion.SampleID,
gs_1_week_finalUnion.Operator, Format$([TestDate],'m/dd/yyyy') AS Test_Date,
gs_1_week_finalUnion.Test FROM Info_ME_Employees INNER JOIN gs_1_week_finalUnion ON
Info_ME_Employees.Full_Name = gs_1_week_finalUnion.Operator" & _
" WHERE Info_ME_Employees.ID IN (4,5,6,7)AND gs_1_week_finalUnion.TestDate Between (Date()-7-
Weekday(Date(),2)) And (Date()-Weekday(Date(),2)-1) " & _
" ORDER BY gs_1_week_finalUnion.Operator"
strSQL2 = "SELECT Info_ME_Employees.ID, Info_ME_Employees.Full_Name FROM Info_ME_Employees" & _
" WHERE Info_ME_Employees.ID IN (4,5,6,7)"
qdf.SQL = strSQL
qdf2.SQL = strSQL2
DoCmd.OpenReport "Operator Daily Review", acViewReport
Set db = Nothing
Set qdf = Nothing
End Sub

I think that there is a limit of tables that can be included in a UNION query - possibly 32. Therefore your options are:
Create several UNION queries, and then UNION them all together as the final step;
Insert the data into a temp table using each individual part of the union query.
Additionally, there may be some way that your database could be re-designed, as it is quite unusual to have to have some many unions needed.
Regards,

Actually, the statement for this "error" is incorrect!
“Cannot open any more databases.” What microsoft should have said here is that no more links to a database can be opened. That is why adding more UNIONs caused this error. Because each separate reference to a link to an object (table or query) causes another link (microsoft uses the term "database") to be opened.

Related

Access 2010 - Determine existence of record in one of three remote mdb files for subsequent select

There are three mdb files in folders on a network drive that may hold the required record(s). How do I determine which db holds the record(s), ideally without data transfer/linking/etc.? Then a single SQL or DAO select can get the data from the correct db. Note: I'm trying to use Access as a front end to SQL using existing Access data spread all around the network drives.
My current solution of configuring 3 DAO objects and checking for no results, in succession until found, seems to load the remote tables to the local recordset and takes too long.
Is there a way to use IF EXISTS in this scenario?
This code throws "Invalid SQL statement; expected DELETE,INSERT,PROCEDURE,SELECT,OR UPDATE" error but is generally what I'd like to do :
Dim strSQL As String
Dim strSku As String
Dim intDbToSearch As Integer
strSku = DLookup("SKUNo", "tblCurrentItem") 'Note: this returns valid SKU#
strSQL = "IF EXISTS(SELECT xxTable.SKUNo "
strSQL = strSQL & "FROM [S:\Our Inventory\Cust Sleeves.mdb].[xxTable] "
strSQL = strSQL & "Where xxTable.SKUNo = " & "'" & strSku & "') Then intDbToSearch = 1"
DoCmd.RunSQL strSQL
This is one of three IF Exists that would run if SKUNo not found in db 1 or 2.
Ultimately intDbToSearch should point to db 1,2,or 3 if SKUNo found or 0 if not.
Thanks
In the end, I pushed usage rules for the 3 databases upstream and can now predetermine which database to search. Thanks again for your input.
Will the sought for SKU always occur in only 1 of the tables?
If you don't want to set table links or use VBA recordsets, only other approach I can see is a query object with a dynamic parameter that references a form control for the SKU input. No idea if this will be faster and will need a query for each remote table.
SELECT SKUNo FROM xxTable IN "S:\Our Inventory\Cust Sleeves.mdb" WHERE SKUNo = Forms!formname!cbxSKU
Then just do DCount on the query.
Dim intDbToSearch As Integer
If DCount("*", "xxQuery") > 0 Then
intDbToSearch = 1
End If
Could UNION the SELECT statements so would have only 1 query object to work with.
SELECT "x1" AS Source, SKUNo FROM xxTable IN "S:\Our Inventory 1\Cust Sleeves.mdb" WHERE SKUNo = Forms!formname!cbxSKU
UNION SELECT "x2", SKUNo FROM xxTable IN "S:\Our Inventory 2\Cust Sleeves.mdb" WHERE SKUNo = Forms!formname!cbxSKU
UNION SELECT "x3", SKUNo FROM xxTable IN "S:\Our Inventory 3\Cust Sleeves.mdb" WHERE SKUNo = Forms!formname!cbxSKU;
How about a simple Function to check if exists by passing the table name and value?
Something like this:
Public Function ExistInTable(Byval TableName As String, ByVal Value As String) As Boolean
ExistInTable = (DCount("*", TableName, "[SKUNo]='" & Value & "'" > 0)
End Function
To call it:
Sub Test()
If ExistInTable("T1", "Whatever") Then 'Exists in T1
If ExistInTable("T2", "Whatever") Then 'Exists in T2
'....
End Sub

How can I add criteria based on a form field to an Access query?

How do I get an operator to work in a query criteria based on a form field. Ideally I would like it to be something like:
IIf([Afloat]="No",<[Forms]![DASF]![Text222],"")
When I remove the operator it finds anything exactly to the criteria in that field but the moment I try to put an operator like greater than or less than it does not work. I am trying to find all records less than the value in that form field.
Any advice on how I can fix this? Or is it not possible in MS Access?
QBF (Query By Form) can't accept operators in the formula. Your only option is to write the query on the fly. You can use the CreateQueryDef method to define the SQL in a specific query, and attach your form or report to the specific query name.
Something like:
Dim db as Database
Dim rec as Recordset
Dim qdf As QueryDef
Dim strSQL as String
Set db = CurrentDB
On Error Resume Next
'First, delete the query if it exists
db.QueryDefs.Delete "MyQueryName"
'Then, set up the query string
strSQL = "Select * From MyTable Where MyField < " & [Forms]![DASF]![Text222] & " and [Afloat] = 'No' "
strSQL = strSQL & "UNION "
strSQL = strSQL & "Select * From MyTable Where MyField = '' and [Afloat] <> 'No' "
'Now, recreate the query
Set qdf = db.CreateQueryDef("MyQueryName", strSQL)
DoCmd.OpenQuery qdf.Name
You could try changing the first criteria to:
>IIf([Afloat]="No",[Forms]![DASF]![Text222])
And then add a second criteria below it in the Or line:
=IIf([Afloat]<>"No","")
I ended up solving my problem by separating it into two separate queries. Below are my steps:
Instead of having a logical expression to decide I separated it into
FLOAT and NONFLOAT queries.
Then I created a command button to open
each query depending on the criteria in a combo box (yes or no).
Here is the code:
Private Sub Command2_Click()
DoCmd.SetWarnings False
If Me.Combo272 = "Yes" Then
DoCmd.OpenQuery "DASF_AGED_AS1_FLOAT", acViewNormal, acEdit
Else
DoCmd.OpenQuery "DASF_AGED_AS1_NONFLOAT", acViewNormal, acEdit
End If
End Sub
This created another problem, I was still unable to reference the txt boxes necessary for my query criteria. To solve this, I made all the text boxes unbound by using the below VBA to auto populate the text boxes based on another combo box. Here is the VBA I used:
Me.Text220 = DLookup("REGION", "TDD_TABLE", "[ID]= " & Me.Combo236)

Adding a new record with VBA

I have a form in which one of the ComboBoxes lists all the documents of a given project. The user should select one and after pressing a button, and if present in Table Dessinsit opens a second form showing that record. If it is not present in that table, I want to add it in.
One of my collegues told me all I had to do was to execute an SQL query with VBA. What I have so far is this:
Dim rsDessin As DAO.Recordset
Dim strContrat As String
Dim strProjet As String
Dim strDessin As String
Dim sqlquery As String
'I think these next 3 lines are unimportant. I set a first query to get information I need from another table
strDessin = Me.Combo_Dessin
strProjet = Me.Combo_Projet
sqlquery = "SELECT [Projet HNA] FROM [Projets] WHERE [Projet AHNS] = '" & strProjet & "'"
Set rsDessin = CurrentDb.OpenRecordset(sqlquery)
If Not rsDessin.RecordCount > 0 Then 'If not present I want to add it
strContrat = rsDessin![Projet HNA]
sqlquery = "INSERT INTO Feuilles ([AHNS], [Contrat], [No Projet]) VALUES (strDessin, strContrat, strDessin)"
'Not sure what to do with this query or how to make sure it worked.
End If
'Checking my variables
Debug.Print strProjet
Debug.Print strContrat
Debug.Print strDessin
'By here I'd like to have inserted my new record.
rsDessin.Close
Set rsDessin = Nothing
I also read online that i could achieve a similar result with something like this:
Set R = CurrentDb.OpenRecordset("SELECT * FROM [Dessins]")
R.AddNew
R![Contrat] = strContrat
R![Projet] = strProjet
R![AHNS] = strDessin
R.Update
R.Close
Set R = Nothing
DoCmd.Close
Is one way better than the other? In the case where my INSERT INTO query is better, what should I do to execute it?
You're asking which is preferable when inserting a record: to use an SQL statement issued to the Database object, or to use the methods of the Recordset object.
For a single record, it doesn't matter. However, you could issue the INSERT statement like this:
CurrentDb.Execute "INSERT INTO Feuilles ([AHNS], [Contrat], [No Projet]) VALUES (" & strDessin & ", " & strContrat & ", " & strDessin & ")", dbFailOnError
(You should use the dbFailOnError option to catch certain errors, as HansUp points out in this answer.)
For inserting multiple records from another table or query, it is generally faster and more efficient to issue an SQL statement like this:
Dim sql = _
"INSERT INTO DestinationTable (Field1, Field2, Field3) " & _
"SELECT Field1, Field2, Field3 " & _
"FROM SourceTable"
CurrentDb.Execute sql
than the equivalent using the Recordset object:
Dim rsSource As DAO.Recordset, rsDestination As DAO.Recordset
Set rsSource = CurrentDb.OpenRecordset("SourceTable")
Set rsDestination = CurrentDb.OpenRecordset("DestinationTable")
Do Until rs.EOF
rsDestination.AddNew
rsDestination!Field1 = rsSource!Field1
rsDestination!Field2 = rsSource!Field2
rsDestination!Field3 = rsSource!Field3
rsDestination.Update
rs.MoveNext
Loop
That said, using an SQL statement has its limitations:
You are limited to SQL syntax and functions.
This is partially mitigated in Access, because SQL statements can use many VBA built-in functions or functions that you define.
SQL statements are designed to work on blocks of rows. Per-row logic is harder to express using only the Iif, Choose, or Switch functions; and logic that depends on the current state (e.g. insert every other record) is harder or impossible using pure SQL. This can be easily done using the Recordset methods approach.
This too can be enabled using a combination of VBA and SQL, if you have functions that persist state in module-level variables. One caveat: you'll need to reset the state each time before issuing the SQL statement. See here for an example.
One part* of your question asked about INSERT vs. Recordset.AddNew to add one row. I suggest this recordset approach:
Dim db As DAO.Database
Dim R As DAO.Recordset
Set db = CurrentDb
Set R = db.OpenRecordset("Dessins", dbOpenTable, dbAppendOnly)
With R
.AddNew
!Contrat = rsDessin![Projet HNA].Value
!Projet = Me.Combo_Projet.Value
!AHNS = Me.Combo_Dessin.Value
.Update
.Close
End With
* You also asked how to execute an INSERT. Use the DAO.Database.Execute method which Zev recommended and include the dbFailOnError option. That will add clarity about certain insert failures. For example, a key violation error could otherwise make your INSERT fail silently. But including dbFailOnError ensures you get notified about the problem immediately. So always include that option ... except in cases where you actually want to allow an INSERT to fail silently. (For me, that's never.)

How to run parameterized query from VBA. Parameters sourced from recordset

I have a form where a user selects a vendor's name from a combobox, whose catalog file is to be imported. The combobox selection then drives a query to create a one-record recordset (rsProfile) containing several profile variables queried from a table of all vendor profiles. These variables are then used in a series of different queries to reformat, translate and normalize the vendor's uniquely structured files to a standardized format that can be imported into our system.
I am frustrated that I can't figure out how to build my stored queries that will use one or more parameters that are automatically populated from the profile recordset.
Here is my rsProfile harvesting code. It works. Note that intVdrProfileID is a global variable set and used in other places.
Private Sub btn_Process_Click()
Dim ws As Workspace
Dim db, dbBkp As DAO.Database
Dim qdf As DAO.QueryDef
Dim rsProfile, rsSubscrip As Recordset
Dim strSQL As String
Dim strBkpDBName As String
Dim strBkpDBFullName As String
strBkpDBName = Left(strVdrImportFileName, InStr(strVdrImportFileName, ".") - 1) & "BkpDB.mdb"
strBkpDBFullName = strBkpFilePath & "\" & strBkpDBName
Set db = CurrentDb
Set ws = DBEngine.Workspaces(0)
MsgBox ("Vendor Profile ID = " & intVdrProfileID & vbCrLf & vbCrLf & "Backup file path: " & strBkpFilePath)
' Harvest Vendor Profile fields used in this sub
strSQL = "SELECT VendorID, Div, VPNPrefix, ImportTemplate, " & _
"VenSrcID, VenClaID, ProTyp, ProSeq, ProOrdPkg, ProOrdPkgTyp, JdeSRP4Code, " & _
"PriceMeth, " & _
"ProCost1Frml, ProCost2Frml, " & _
"ProAmt1Frml, ProAmt2Frml, ProAmt3Frml, ProAmt4Frml, ProAmt5Frml " & _
"FROM tZ100_VendorProfiles " & _
"WHERE VendorID = " & intVdrProfileID & ";"
Set qdf = db.QueryDefs("qZ140_GetProfileProcessParms")
qdf.SQL = strSQL
Set rsProfile = qdf.OpenRecordset(dbOpenSnapshot)
DoCmd.OpenQuery "qZ140_GetProfileProcessParms"
' MsgBox (qdf.SQL)
I have used QueryDefs to rewrite stored queries at runtime, and although it works, it is quite cumbersome and does not work for everything.
I was hoping for something like the sample below as a stored query using DLookups. I can get this to work in VBA, but I can't get anything to work with stored queries. I am open to other suggestions.
Stored Query "qP0060c_DirectImportTape":
SELECT
DLookUp("[VPNPrefix]","rsProfile","[VendorID]=" & intVdrProfileID) & [PartNo] AS VenPrtId,
Description AS Des,
DLookup("[Jobber]","rsProfile",[VendorID=" & intVdrProfileID) AS Amt1,
INTO tP006_DirectImportTape
FROM tJ000_VendorFileIn;
ADDENDUM:
Let me adjust the problem to make it a bit more complex. I have a collection of about 40 queries each of which use a different collection of parameters (or none). I also have a table containing the particular set of queries that each vendor 'subscribes' to. The goal is to have a database where a non-coding user can add new vendor profiles and create/modify the particular set of queries which would be run against that vendor file. I have almost 100 vendors so far, so coding every vendor seperately is not practical. Each vendor file will be subjected to an average of 14 different update queries.
Simplified Example:
Vendor1 file needs to be processed with queries 1, 2 and 5. Vendor2 file might need only update queries 2 and 4. The parameters for these queries might be as follows:
query1 (parm1)
query2 (parm1, parm4, parm8, parm11)
query4 (parm5, parm6, parm7, parm8, parm9, parm10, parm11)
query5 () -no parms required
This is the core query processing that loops through only the queries relevant to the current vendor file. rsSubscrip is the recordset (queried from a master table) containing this filtered list of queries.
' Run all subscribed queries
MsgBox "Ready to process query subscription list."
With rsSubscrip
Do While Not .EOF
db.Execute !QueryName, dbFailOnError
.MoveNext
Loop
.Close
End With
You can set the parameters of a predefined query using the syntax;
Set qdf = CurrentDB.QueryDefs(QueryName)
qdf.Parameters(ParameterName) = MyValue
To add parameters to the query, add the following before the SELECT statement in the sql
PARAMETERS [ParameterOne] DataType, [ParameterTwo] DataType;
SELECT * FROM tblTest;

SQL Select Query via Excel VBA - Defining Parameters, query of a query

I need to place 3 Parameters into a Select query from Excel VBA to SQL - One of which can just be replaced using a variable. But this query is a query of another query, and the parameters are held within those two other queries. If running this query in Access I'm just prompted by all three to manually type them in - "Start", "End", and "AdvisorName".
Running the code will prompt the "No value given for one or more required parameters" - However, only 1 Parameter is in this query, the other 2 parameters are held within the other two queries inside this query - "Q_SoloFocus_Advisor_QuestionsYes" and "Q_SoloFocus_Advisor_QuestionsNo".
The three parameters are called "Start" (Start Date range), "End" (End Date range), and "AdvisorName" (held within this query).
Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset
Dim Command As New ADODB.Command
Dim strSQL As String
Set cnn = New ADODB.Connection
cnn.Open ConnectionString:=Cnct
Set rst = New ADODB.Recordset
strSQL = "SELECT Q_SoloFocus_Advisor_QuestionsAll.Advisor,
Q_SoloFocus_Advisor_QuestionsAll.KeyID,
Q_SoloFocus_Advisor_QuestionsAll.SubQ_Text,
Q_SoloFocus_Advisor_QuestionsAll.CountOfAnswer AS [All],
Q_SoloFocus_Advisor_QuestionsNo.CountOfAnswer AS [No],
Q_SoloFocus_Advisor_QuestionsYes.CountOfAnswer
AS Yes," & _"Format(Q_SoloFocus_Advisor_QuestionsYes.CountOfAnswer/
Q_SoloFocus_Advisor_QuestionsAll.CountOfAnswer,'0.0%')
AS Result" & _"
FROM (Q_SoloFocus_Advisor_QuestionsAll
LEFT JOIN Q_SoloFocus_Advisor_QuestionsNo
ON (Q_SoloFocus_Advisor_QuestionsAll.SubQ_Text =
Q_SoloFocus_Advisor_QuestionsNo.SubQ_Text)
AND (Q_SoloFocus_Advisor_QuestionsAll.KeyID =
Q_SoloFocus_Advisor_QuestionsNo.KeyID)
AND (Q_SoloFocus_Advisor_QuestionsAll.Advisor =
Q_SoloFocus_Advisor_QuestionsNo.Advisor))
LEFT JOIN Q_SoloFocus_Advisor_QuestionsYes
ON (Q_SoloFocus_Advisor_QuestionsAll.SubQ_Text =
Q_SoloFocus_Advisor_QuestionsYes.SubQ_Text)
AND (Q_SoloFocus_Advisor_QuestionsAll.Advisor =
Q_SoloFocus_Advisor_QuestionsYes.Advisor)
AND (Q_SoloFocus_Advisor_QuestionsAll.KeyID =
Q_SoloFocus_Advisor_QuestionsYes.KeyID)" & _
" WHERE (((Q_SoloFocus_Advisor_QuestionsAll.Advisor)=[AdvisorName]));"
'----------------------------------------------------------------------------------
'----------------------------------------------------------------------------------
rst.Open strSQL, cnn, adOpenStatic
rst.MoveFirst
The SQL for the other two queries are similar, one has a where "No" and the other a where not "No" where the parameters are required are:
SELECT tbl_Surveys.Advisor, tbl_QuestionRef.KeyID, tbl_QuestionRef.CallSkill,
tbl_QuestionRef.CallReason, tbl_QuestionRef.MainQ_Text
tbl_QuestionRef.SubQ_Text,
Count(tbl_SurveyAnswers.Answer) AS CountOfAnswer
FROM tbl_QuestionRef
INNER JOIN (tbl_SurveyAnswers
INNER JOIN tbl_Surveys
ON tbl_SurveyAnswers.SurveyLink = tbl_Surveys.ID)
ON tbl_QuestionRef.KeyID = tbl_SurveyAnswers.QuestionRef
WHERE (((tbl_SurveyAnswers.Answer)<>"No"
And (tbl_SurveyAnswers.Answer)<>"N/A"
And (tbl_SurveyAnswers.Answer)<>"0")
AND ((tbl_Surveys.CallDate)>=[Start]
And (tbl_Surveys.CallDate)<=[End]))
GROUP BY tbl_Surveys.Advisor, tbl_QuestionRef.KeyID, tbl_QuestionRef.CallSkill,
tbl_QuestionRef.CallReason,
tbl_QuestionRef.MainQ_Text, tbl_QuestionRef.SubQ_Text;
As you can see, the "Start" and "End" parameters are in this second SQL query... Any ideas how I can put these two parameters into the first SQL function? I can't put the Start and End into the "All" query, as it'll knock out the "Count" part...
Assuming the other two queries are saved and named, instead of calling named queries, incorporate the SQL from the two queries into the main query as derived tables. By doing so, the query will prompt for all parameters. See the JOIN clauses for some clarification.
strSQL = "SELECT Q_SoloFocus_Advisor_QuestionsAll.Advisor, "
Q_SoloFocus_Advisor_QuestionsAll.KeyID,
Q_SoloFocus_Advisor_QuestionsAll.SubQ_Text,
Q_SoloFocus_Advisor_QuestionsAll.CountOfAnswer AS [All],
Q_SoloFocus_Advisor_QuestionsNo.CountOfAnswer AS [No],
Q_SoloFocus_Advisor_QuestionsYes.CountOfAnswer
AS Yes," & _"Format(Q_SoloFocus_Advisor_QuestionsYes.CountOfAnswer/
Q_SoloFocus_Advisor_QuestionsAll.CountOfAnswer,'0.0%')
AS Result" & _"
FROM (Q_SoloFocus_Advisor_QuestionsAll
LEFT JOIN (YOUR FIRST SAVE QUERY SQL) AS Q_SoloFocus_Advisor_QuestionsNo
ON (Q_SoloFocus_Advisor_QuestionsAll.SubQ_Text =
Q_SoloFocus_Advisor_QuestionsNo.SubQ_Text)
AND (Q_SoloFocus_Advisor_QuestionsAll.KeyID =
Q_SoloFocus_Advisor_QuestionsNo.KeyID)
AND (Q_SoloFocus_Advisor_QuestionsAll.Advisor =
Q_SoloFocus_Advisor_QuestionsNo.Advisor))
LEFT JOIN (YOUR SECOND QUWERY SQL) AS Q_SoloFocus_Advisor_QuestionsYes
ON (Q_SoloFocus_Advisor_QuestionsAll.SubQ_Text =
Q_SoloFocus_Advisor_QuestionsYes.SubQ_Text)
AND (Q_SoloFocus_Advisor_QuestionsAll.Advisor =
Q_SoloFocus_Advisor_QuestionsYes.Advisor)
AND (Q_SoloFocus_Advisor_QuestionsAll.KeyID =
Q_SoloFocus_Advisor_QuestionsYes.KeyID)" & _
" WHERE (((Q_SoloFocus_Advisor_QuestionsAll.Advisor)=[AdvisorName]));"