I have two sets of code, that are the same I just change variables to another set that exist and now with the ones I changed I get an error saying "Run-time error '3061': Too few parameters. Expected 6."
This is the changed code:
Dim rec As Recordset
Dim db As Database
Dim X As Variant
Set db = CurrentDb
Set rec = db.OpenRecordset("UnitMoreInfoQ")
Const msgTitle As String = "Open Explorer"
Const cExplorerPath As String = "C:\WINDOWS\EXPLORER.EXE"
Const cExplorerSwitches As String = " /n,/e"
cFilePath = rec("ProjFilePath")
It highlights this line:
Set rec = db.OpenRecordset("UnitMoreInfoQ")
This is the first code:
Dim rec As Recordset
Dim db As Database
Dim X As Variant
Set db = CurrentDb
Set rec = db.OpenRecordset("ProjectMoreInfoQ")
Const msgTitle As String = "Open Explorer"
Const cExplorerPath As String = "C:\WINDOWS\EXPLORER.EXE"
Const cExplorerSwitches As String = " /n,/e"
cFilePath = rec("ProjFilePath")
As you can see, the line has the same amount of parameters:
Set rec = db.OpenRecordset("ProjectMoreInfoQ")
This has gotten me quite confused for awhile because of this. How do I fix this error?
I didn't get the same result as you when testing your db, and I still don't understand the difference. However, maybe we can still get you something which works in spite of my confusion.
The query contains 6 references to form controls, such as [Forms]![WorkOrderDatabaseF]![Text71]. Although you're certain that form is open in Form View when you hit the "too few parameters" error at db.OpenRecordset("UnitMoreInfoQ"), Access doesn't retrieve the values and expects you to supply them.
So revise the code to supply those parameter values.
Dim rec As DAO.Recordset
Dim db As DAO.database
Dim prm As DAO.Parameter
Dim qdf As DAO.QueryDef
Dim X As Variant
Set db = CurrentDb
'Set rec = db.OpenRecordset("UnitMoreInfoQ")
Set qdf = db.QueryDefs("UnitMoreInfoQ")
For Each prm In qdf.Parameters
prm.value = Eval(prm.Name)
Next
Set rec = qdf.OpenRecordset(dbOpenDynaset) ' adjust options as needed
I'm leaving the remainder of this original answer below in case it may be useful for anyone else trying to work through a similar problem. But my best guess is this code change will get you what you want, and it should work if that form is open in Form View.
Run this statement in the Immediate window. (You can use Ctrl+g to open the Immediate window.)
DoCmd.OpenQuery "UnitMoreInfoQ"
When Access opens the query, it will ask you to supply a value for the first parameter it identifies. The name of that parameter is included in the parameter input dialog. It will ask for values for each of the parameters.
Compare those "parameter names" to your query's SQL. Generally something is misspelled.
Using the copy of your db, DoCmd.OpenQuery("UnitMoreInfoQ") asks me for 6 parameters.
Here is what I see in the Immediate window:
? CurrentDb.QueryDefs("UnitMoreInfoQ").Parameters.Count
6
for each prm in CurrentDb.QueryDefs("UnitMoreInfoQ").Parameters : _
? prm.name : next
[Forms]![WorkOrderDatabaseF]![Text71]
[Forms]![WorkOrderDatabaseF]![ClientNameTxt]
[Forms]![WorkOrderDatabaseF]![WorkOrderNumberTxt]
[Forms]![WorkOrderDatabaseF]![TrakwareNumberTxt]
[Forms]![WorkOrderDatabaseF]![WorkOrderCompleteChkBx]
[Forms]![WorkOrderDatabaseF]![WorkOrderDueDateTxt]
Make sure there is a form named WorkOrderDatabaseF open in Form View when you run this code:
Set rec = db.OpenRecordset("UnitMoreInfoQ")
Does the [UnitMoreInfoQ] query execute properly on its own? If you mistype a field in access it will treat that field as a parameter.
ProjectMoreInfoQ and UnitMoreInfoQ are different queries... it sounds like one takes 6 parameters and the other doesn't. Look at the queries in Access and see if either have parameters defined.
Related
I want to show the results of a call to a webservice (several rows) in ms-Access. To do this I created a form with the defaultView = 1 (= continuous form).
Now I wonder if I can use show results from the webservice directly in my form. This means without creating a table which I then select with the recordsource-property.
Is there a way to show data a continuous-form in MS-Access without using a table?
I tried to set the recordset by myself like this:
Private Sub Form_Load()
Set m_Dataset = CurrentDb.OpenRecordset("Test", RecordsetTypeEnum.dbOpenDynamic)
Call m_Dataset.AddNew
m_Dataset("OutOfThinAir") = "Hallo"
Set Me.Recordset = m_Dataset
End Sub
But OpenRecordset raises the error "invalid argument".
I also thought of setting the recordsource to a select statement without using a table name (In oracle this would be "Select ... from dual") but I did not find a working statement. "SELECT 1 from dual;" does definitely not work.
Yes, but you'd need to use an ADODB recordset, not a DAO one.
E.g.
Dim m_Dataset As New ADODB.Recordset
m_Dataset.Fields.Append "OutOfThinAir",adVarWChar, 6, adFldUpdatable
m_Dataset.Open
m_Dataset.AddNew 'No call!
m_Dataset("OutOfThinAir") = "Hallo"
Set Me.Recordset = m_Dataset
With the help of Erik A I I figured out a solution which works:
Private Sub Form_Load()
Dim rstADO As ADODB.Recordset
Set rstADO = New ADODB.Recordset
rstADO.Fields.Append "OutOfThinAir", adVarChar, 100, adFldMayBeNull
rstADO.LockType = adLockOptimistic
rstADO.Open
rstADO.Addnew
rstADO.Fields("OutOfThinAir") = "Hello"
rstADO.Update
rstADO.Addnew
rstADO.Fields("OutOfThinAir") = "Du"
rstADO.Update
Set Me.Recordset = rstADO
End Sub
Btw I had to add "Microsoft ActiveX Data Objects 6.1 Library" as a reference in order to use the constants and "ADODB.Recordset" as a variabletype.
I am trying to create some custom buttons in Outlook that interact with a table contained within an Access database. So far I have my buttons working in Outlook, running code that instantiates a custom data access class which in turn handles opening and closing the connection to the database. So far as I can tell, this much works.
However from this class I cannot even perform a simple select query. Can anyone help me understand why the code below might not work? I always end out with a recordset that has no rows but if I run the same sql using the Access query designer it works fine.
Public Function GetJobID(ByVal xEmailID As String) As Integer
'Returns the JobID associated with a given EmailID from the email link table.
'Returns a fail constant if no link exists.
Dim rs As ADODB.Recordset
Dim sql As String
'Exit if not connected.
'Cast to boolean because VBA doesn't recognise connection state integer as boolean.
If Not CBool(mConn.State) Then
GetJobID = RESULT_FAIL_INTEGER
Exit Function
End If
sql = "SELECT [JobID] FROM [EMAIL_LINK_TABLE] WHERE [EmailID]='xEmailID'"
sql = Replace(sql, "EMAIL_LINK_TABLE", EMAIL_LINK_TABLE)
sql = Replace(sql, "xEmailID", xEmailID)
On Error Resume Next
Set rs = mConn.Execute(sql)
If rs.RecordCount > 0 Then
GetJobID = rs(1).Value
Else
GetJobID = RESULT_FAIL_INTEGER
End If
End Function
I see you've tracked down the issue to .RecordCount returning -1.
This is standard behavior for dynamic cursors, from the docs:
The cursor type of the Recordset object affects whether the number of records can be determined. The RecordCount property will return -1 for a forward-only cursor; the actual count for a static or keyset cursor; and either -1 or the actual count for a dynamic cursor, depending on the data source.
Of course, you can modify your code to use a static cursor, but that will impact performance. Instead, to test if there are records in your recordset, use .EOF (a method returning a boolean to indicate if the recordset is currently at the end of the file). That will save your code from having to load all records, when only loading the first one is required:
Public Function GetJobID(ByVal xEmailID As String) As Integer
'Returns the JobID associated with a given EmailID from the email link table.
'Returns a fail constant if no link exists.
Dim rs As ADODB.Recordset
Dim sql As String
'Exit if not connected.
'Cast to boolean because VBA doesn't recognise connection state integer as boolean.
If Not CBool(mConn.State) Then
GetJobID = RESULT_FAIL_INTEGER
Exit Function
End If
sql = "SELECT [JobID] FROM [EMAIL_LINK_TABLE] WHERE [EmailID]='xEmailID'"
sql = Replace(sql, "EMAIL_LINK_TABLE", EMAIL_LINK_TABLE)
sql = Replace(sql, "xEmailID", xEmailID)
On Error Resume Next
Set rs = mConn.Execute(sql)
If Not rs.EOF Then
GetJobID = rs(0).Value
Else
GetJobID = RESULT_FAIL_INTEGER
End If
End Function
I have an Access database divided into front and back ends.
I need to modify the value of a property associated to one of the fields in a table programmatically. I do remember achieving something similar years ago, but that was for forms.
It seems that the properties of a table can only be set at design time; any attempt to modify the values using code (myField.Properties("InputMask").Value = "000000") causes an error.
All in all, there are about 40 or 50 tables in a batch of roughly 80 that have a particular type of field that has to be changed, so I'd rather do this using code than manually. Could anyone suggest a method for doing this using VBA, please?
Presently I've looked at dropping and recreating the field using CurrentDb.Execute sqlString, but I'd like to retain the InputMask property if at all possible.
The original database is a 2002/3 format, but I'm editing this in Access 2010.
I got the following code to work. It will change the InputMask of the specified Table and Field....
Option Compare Database
Option Explicit
Sub Test_It()
Dim WGD
WGD = Resize_And_AddInputMask("Table3", "SomeNbr")
End Sub
' Modified version of code found at: http://www.tek-tips.com/viewthread.cfm?qid=1708626
Public Function Resize_And_AddInputMask(ByVal someTableName As String, ByVal someZCField As String)
Dim db As DAO.Database
Dim td As DAO.TableDef
Dim fd As DAO.Field
Dim prp As DAO.Property
Set db = CurrentDb
Set td = db.TableDefs(someTableName)
Set fd = td.Fields(someZCField)
fd.Properties("InputMask") = "000099"
fd.Properties.Refresh
db.TableDefs.Refresh
Set prp = Nothing
Set fd = Nothing
Set td = Nothing
Set db = Nothing
End Function
I'm working in Access and trying to use a query with parameters in VBA. I have several queries that I need to use, so I added a routine to generalize the process:
Public Function Execute_query(query) As Recordset
Dim qdf As QueryDef
Set qdf = CurrentDb.QueryDefs(query)
For Each prm In qdf.Parameters
prm.Value = Eval(prm.Name)
Next prm
If (qdf.Type = 80) Then
qdf.Execute
Else: Set Execute_query = qdf.OpenRecordset
End If
End Function
I'm still testing this so there may be other issues, but my immediate question is why the Eval(prm.name) line isn't working. The paramater is [R_Yr] which I've declared as a public variable and have assigned a value - which I can verify in the watch window. But I get an error code 2482 - Access cannot find the name 'R_yr"
This same code seems to work when the parameter value is coming from a form instead of a variable - which is why I had to set it up in the first place - I couldn't access a form control in a query run from VBA.
Forgive me, but I'm having a bit of trouble seeing the real benefit from this extra level of indirection. You have to create variables that correspond to the parameters for the particular query you are planning to invoke, and then you call a generic function to invoke it. Why not just create a QueryDef, pass it the parameters, and invoke it in place? It seems like essentially the same amount of work, and it makes the code easier to follow because "everything is right there".
Eval() takes the string you give it and uses the "expression service" to process it. The problem you're facing is the expression service doesn't know anything about VBA variables. If you're really determined, you may be able to figure out a workaround which builds the variable's value rather than the variable's name into the string you give Eval() to ... um ... evaluate.
But for what you're doing, I suggest you ditch Eval(). Instead give the function a data structure such as a Scripting.Dictionary or VBA Collection which contains the parameter values with your former variable names as keys.
Here is a VBA Collection example ...
Dim MyCol As Collection
Set MyCol = New Collection
MyCol.Add CLng(10), "R_Yr"
MyCol.Add "foo", "MyString"
Debug.Print MyCol("R_Yr"), TypeName(MyCol("R_Yr"))
Debug.Print MyCol("MyString"), TypeName(MyCol("MyString"))
That code gives me this output in the Immediate window ...
10 Long
foo String
So consider building a similar collection in the calling code and passing that collection to a modified Execute_query function.
Public Function Execute_query(ByVal pQdf As String, _
ByRef pCol As Collection) As Recordset
Dim qdf As QueryDef
Set qdf = CurrentDb.QueryDefs(pQdf)
For Each prm In qdf.Parameters
prm.Value = pCol(prm.Name)
Next prm
If (qdf.Type = 80) Then
qdf.Execute
Else
Set Execute_query = qdf.OpenRecordset
End If
End Function
I have an Access project where I want a label to be showed when a form is opened only if a query returns a result.
I have the following code:
Private Sub Form_Load()
Dim stSQL As String
Dim db As DAO.Database
Dim rs As DAO.Recordset
Set db = DBEngine.Workspaces(0).Databases(0)
Dim cn As DAO.Connection
Set cn = DAO.Connection
cn.Provider = "Microsoft.Jet.OLEDB.4.0"
cn.Open stdbName
stSQL1 = "SELECT * FROM tbl_lessons"
Set rs = db.OpenRecordset(stSQL1, dbOpenDynaset)
If (rs Is Not Nothing) Then
If (rs.GetRows() > 0) Then
lbl_alert.Visible = True
Else
lbl_alert.Visible = False
End If
End If
When I try to open the form I'm getting the following error:
Compile error:
Method or data member not found
I'm using Access 2007 with VB7
Can someone please help?
Note - when compile errors happen in VBA, a line of code is always highlighted. Looking carefully at the highlighted line will help you figure out what you did wrong. Also note that you should always compile your code before attempting to run the form. (open the "Debug" menu > click "Compile VBAProject" or the like.)
There appears to be a bunch of problems, and you'll probably have to address them one at a time. just keep fixing issues and re-compiling your code.
1cn.Open stdbName1
--> stdbname is not defined anywhere in the code you showed us.
Dim stSQL As String
--> You defined your connection string as stSQL but in your code you used: stSQL1 = "...". Fix your variable name.