Run-time error '3061' Too few parameters. Expected 1 - ms-access

Keep getting a run-time error 3061 which the following script in Acces. Debugger reports a problem with last line but for the life of me I cannot see what the issue is. Any assistance greatly appreciated.
Dim db As Database
Dim salers As DAO.Recordset 'Sale
Set db = CurrentDb
Dim saleQuery As String
saleQuery = "SELECT * FROM Sales WHERE salesID = " & Me.saleID.Value & ";"
Set salers = db.OpenRecordset(saleQuery)

This error almost always means something is miss-spelled, or doesn't exist. So, ensure that Sales and salesID exist and are spelled properly in the query.
If that is OK, look at Me.saleID.Value. If this is a string, the query will have enclose the value in quotes:
saleQuery = "SELECT * FROM Sales WHERE salesID = '" & Me.saleID.Value & "';"
Note the ' around the saleID value.

Related

Merge 2 fields and insert into table

hi i am trying to insert value into my output table 
in my Input table have 
profit  extra 
10         20 
when i insert into my output table it should get concatenated as 
cost 
1020 
sub test()
Dim db As DAO.Database
Dim rst As DAO.Recordset
Set db = CurrentDb
db. execut "Insert into OUTPUT_TBL (DESCRIPTION,COST,DEBIT,CREDIT) " & _
" SELECT INPUT.DESCRIPTION,((INPUT.PROFIT)+(INPUT.EXTRA)) AS COST," & _
" IIF(EXTERNAL.SOLUTION='DEBIT',(AMOUNT),0) as DEBIT, " & _
" IIF(EXTERNAL.SOLUTION='CREDIT',(AMOUNT),0) AS CREDIT " & _
" FROM INPUT , EXTERNAL"
db.close
end test
when i try to run it i am getting error as  run time error 3075
Couple issues - noticed a typo, it should be db.execute "" not db.execut
Also, for string concatenation use & in Access SQL. 3075 means you used a bad operator.
Another thing, You may also need to add a JOIN to the SQL.
For example, to get you on the right track:
db.execute "Insert into OUTPUT_TBL (DESCRIPTION,COST,DEBIT,CREDIT) SELECT INPUT.DESCRIPTION,((INPUT.PROFIT)&""&(INPUT.EXTRA)) AS COST,IIF(EXTERNAL.SOLUTION='DEBIT',(AMOUNT),0) as DEBIT, IIF(EXTERNAL.SOLUTION='CREDIT',(AMOUNT),0) AS CREDIT from INPUT JOIN EXTERNAL ON INPUT.KEY=EXTERNAL.KEY"

Can I have a DoCmd method in IF statement?

I have the following VBA code for a .mdb Access file:
If DoCmd.RunSQL "SELECT DISTINCT Max(wk_ending_dt) FROM d2s_loader_performance" < (Date()-Weekday(Date())) Then
DoCmd.RunSQL "DELETE * FROM d2s_loader_performance_tbl WHERE wk_ending_dt = (Date()-Weekday(Date())-35)"
End If
It then highlights the following text: "SELECT DISTINCT Max(wk_ending_dt) FROM d2s_loader_performance" And gives the error Compile Error: Expected Then or GoTo.
Any ideas? I have a Then at the end of my conditional check, and to my understanding the double quotes are just for the SQL syntax. I'm using this If...Then condition to only allow record deletion if the max table date is less than the previous week's ending date.
You probably need to attack it this way:
Dim db as Database
Dim rec as Recordset
Set db = CurrentDB
Set rec = db.OpenRecordset ("SELECT DISTINCT Max(wk_ending_dt) FROM d2s_loader_performance")
If rec(0) < (Date()-Weekday(Date())) Then
DoCmd.RunSQL "DELETE * FROM d2s_loader_performance_tbl WHERE wk_ending_dt = #" & (Date()-Weekday(Date())-35) & "#"
EndIf
I'm assuming that "(Date()-Weekday(Date())-35)" part is supposed to be a calculation, so you need to surround it with ampersands (&), and dates in Access always need to have pound signs (#) before and after them if they're true Date fields.
The problem is that you can execute only action querys with DoCmd.RunSQL. So, you can't execute Select statement. For more info see this
My approach is to using something like this:
Sub SqlExecute()
Dim db As DAO.Database
Dim rsttemp As DAO.Recordset
Set db = CurrentDB
sql = "SELECT DISTINCT Max(wk_ending_dt) FROM d2s_loader_performance"
Set rsttemp = db.OpenRecordset(sql, dbOpenSnapshot)
If rsttemp(0)<(Date()-Weekday(Date())) Then
DoCmd.RunSQL "DELETE * FROM d2s_loader_performance_tbl WHERE wk_ending_dt #=" & (Date()-Weekday(Date())-35) & "#"
End If
Set rsttemp = Nothing
End Function

Looping through an array and posting array to table

I am an old Foxpro programmer and I use to use arrays to post variable fields.
What I am trying to do is I have 15 date fields in the new table I designed.
In my query I have individual records with one date for activity.
I want to compile the 15 different dates for a each Client_id into one record with 15 dates but I can't seem to reference the table data as an array.
I have tried a couple different methods of defining the array but nothing seems to work.
Here is my code that I have. In my table I have 15 date fields named Mail_date1, Mail_date2, Mail_date3, etc.
I tried first defining it just as an array but did not like it; my code always fails when I try to reference the date field in the result table rs2!mdate2 = memdate(intcounter)
How can I reference my result table output fields as an array?
Do I have to put a whole bunch of if statements to load my results?
Seems like a waste.... should be able to load them as an array.
I am a new Access 2007 VBA programmer.
Dim db As DAO.Database
Set db = CurrentDb
Dim rs1 As DAO.Recordset
Dim rs2 As DAO.Recordset
Dim FinTotal, intcounter As Integer
Dim FinMPU, FinVersion As String
Dim mail_date(1 To 15) As Date
Dim memdate(1 To 15) As Date
Dim mdate2 As String
Set rs1 = db.OpenRecordset( _
"SELECT NewFile.MPU_ID, " & _
" NewFile.MAIL_DATE," & _
" NewFile.TOTAL, " & _
" Freight.Version " &_
"FROM Freight " & _
" LEFT JOIN NewFile ON Freight.[MPU ID] = NewFile.MPU_ID " & _
"ORDER BY NewFile.MPU_ID, NewFile.MAIL_DATE")
Set rs2 = db.OpenRecordset("Final")
DoCmd.RunSQL "DELETE Final.* FROM Final;"
intcounter = 1
memdate(intcounter) = rs1!mail_date
FinMPU = rs1!mpu_ID
FinTotal = rs1!total
FinVersion = rs1!Version
rs1.MoveNext
On Error GoTo Error_MayCauseAnError
Do While Not rs1.EOF
Do While Not rs1.EOF _
And memdate(intcounter) <> rs1!mail_date _
And FinMPU = rs1!mpu_ID
intcounter = intcounter + 1
memdate(intcounter) = rs1!mail_date
FinTotal = FinTotal + rs1!total
FinVersion = rs1!Version
FinMPU = rs1!mpu_ID
rs1.MoveNext
Loop
If FinMPU <> rs1!mpu_ID Then
rs2.AddNew
mdate2 = "mail_date" & CStr(intcounter)
rs2!mdate2 = memdate(intcounter)
rs2!total = FinTotal
rs2!mpu_ID = FinMPU
rs2!Version = FinVersion
rs2.Update
FinTotal = rs1!total
FinVersion = rs1!Version
FinMPU = rs1!mpu_ID
intcounter = 1
memdate(intcounter) = rs1!mail_date
End If
rs1.MoveNext
Loop
first, if you expect and answer, you should really spend more time on properly formatting your explanation and your code...
Now, for some remarks and possible answer to the question:
You should DELETE FROM Final before you open that table in a recordset.
You should be explicit about the type of recordset you are opening:
' Open as Read-only '
Set rs1 = db.OpenRecordSet("...", dbOpenSnapshot)
' Open as Read/Write '
Set rs1 = db.OpenRecordSet("...", dbOpenDynaset)
You should Dim memdate(1 To 15) As Variant instead of Date as the Date datatype cannot be Null, and since you are pulling data from a LEFT JOIN, it's possible that the returned values could be Null if there are no corresponding data to Freight in the table Newfile.
That On Error GoTo Error_MayCauseAnError should probably not be there.
Use On Error Goto only to catch errors you can't deal with at all.
Using that here will only hide errors in your code. With some proper checks statements you should not even need the On Error Goto...
It looks like your first internal loop is trying to skip some records.
However, when that loop breaks, it could be because it reached EOF, and you never test for that in the code that follows the loop.
You never test if your intcounter goes beyond the 15 allocated dates.
Are you absolutely sure that you can never have more than 15 records?
You do not say which error message you get exactly. That could be useful to help determine the kind of issue at hand.
Instead of
mdate2 = "mail_date" & CStr(intcounter)
rs2!mdate2 = memdate(intcounter)
Use
rs2.Fields("mail_date" & intcounter).Value = memdate(intcounter)
the ! syntax of DAO really only is a shorthand for the longer rs.Fields("name") form.

MS ACESS Error when accessing

I am developing an enrolment system. But i am receiving this error: You tried to execute a query that does not include the specified expression..
I am using the following code:
Private Function RefreshAdvisoryList()
Dim vRS As New ADODB.Recordset
Dim sSQL As String
'clear list
txtsection.Clear
'On Error GoTo ReleaseAndExit
sSQL = "SELECT tblSection.SectionID, tblSection.SectionTitle, tblAdviser.SchoolYear, tblDepartment.DepartmentTitle, tblYearLevel.YearLevelTitle, tblAdviser.TeacherID" & _
" FROM (tblDepartment INNER JOIN (tblYearLevel INNER JOIN tblSection ON tblYearLevel.YearLevelID = tblSection.YearLevelID) ON tblDepartment.DepartmentID = tblSection.DepartmentID) INNER JOIN tblAdviser ON (tblSection.SectionID = tblAdviser.SectionID) AND (tblDepartment.DepartmentID = tblAdviser.DepartmentID)" & _
" GROUP BY tblSection.SectionID, tblSection.SectionTitle, tblAdviser.SchoolYear, tblDepartment.DepartmentTitle, tblYearLevel.YearLevelTitle, tblAdviser.TeacherID" & _
" HAVING (((tblTeacher.TeacherID)='" & curTeacher.TeacherID & "') AND tblSection.SchoolYear='" & Me.txtSchoolYear.Text & "')" & _
" ORDER BY tblAdviser.SchoolYear DESC;"
If ConnectRS(con, vRS, sSQL) = False Then
'fatal
'temp
MsgBox "Unable to connect Teacher's Advisory Recordset.", vbCritical
'GoTo ReleaseAndExit
End If
If AnyRecordExisted(vRS) = True Then
While vRS.EOF = False
txtsection.AddItem vRS!SectionTitle
vRS.MoveNext
Wend
End If
'Exit Function
'ReleaseAndExit:
' Set vRS = Nothing
End Function
Take a look at this screenshot:
The HAVING clause references these 2 fields:
tblTeacher.TeacherID
tblSection.SchoolYear
The SELECT field list includes:
tblAdviser.TeacherID
tblAdviser.SchoolYear
Change the query so that all references to TeacherID come from the same table. Do the same for SchoolYear.
BTW, tblTeacher is not even included among the query's data sources.
If possible, start an Access session and use the query designer to build this query. It will help you avoid this type of error. Once you have a query which works in Access, then adapt your code to produce the same working SQL statement.
Group by has to be used with a aggregate function so that combined result for a group can be retrieved, you have not used any aggregate function.
Refer - http://www.w3schools.com/sql/sql_groupby.asp
Remove group by from your query and add the having clause in the where clause.
Explain what type of data you are expecting so that we can help you in the query.

Possible to join two recordsets using VBA/ADO?

The goal is to join an Access table with matching data from a SQL Server table. I would do this using linked tables in Access but I'm running into Access' BigInt problem (I would have a view created to cast BigInt as Int but that isn't an option right now).
So I've been trying to create two recordsets and join them in VBA/ADO. The left side of the join would be the Access table with Season and WeekNum and the right side of the join would be the SQL Server table with Season and Weeknum and other data. This works fine until I try to create a third recordset that is the result of the join (in this example, I haven't tried to do a join, just the first part of the join by selecting from the Access recordset). I'm getting a Type Mismatch error on the line when I do Set ObjRecordset3 = "SELECT * FROM " & Access_Recordset '.
Is it even possible to join two recordsets? If so, how is this done?
Function Join()
Dim SQL_Server_Connection As ADODB.Connection
Set SQL_Server_Connection = New ADODB.Connection
Dim SQL_Server_Query As String
Dim SQL_Server_Recordset As New ADODB.Recordset
Dim Access_Recordset As New ADODB.Recordset
Dim ObjConnection As ADODB.Connection
Set ObjConnection = CreateObject("ADODB.Connection")
Dim ObjRecordset3 As New ADODB.Recordset
' Get data from Bump Table 3:
Access_Recordset.Open "SELECT * FROM [Bump Table 3]", CurrentProject.Connection
' Open connection to SQL Server:
SQL_Server_Connection_String = "DSN=MySQLServer"
SQL_Server_Connection.Open SQL_Server_Connection_String
' Define the SQL Server query:
SQL_Server_Query = "SELECT Season, WeekNum FROM TE"
' Populate the SQL_Server_Recordset:
SQL_Server_Recordset.Open SQL_Server_Query, SQL_Server_Connection, adOpenDynamic, adLockOptimistic
'Join Access_Recordset (Table: Bump Table 3) to SQL_Server_Recordset (Table: TE)
Set ObjRecordset3 = "SELECT * FROM " & Access_Recordset ' Type Mismatch error on this line
Access_Recordset.Close
Set Access_Recordset = Nothing
SQL_Server_Recordset.Close
Set SQL_Server_Recordset = Nothing
SQL_Server_Connection.Close
End Function
* UPDATE *
I figured out how to get to my ultimate goal which was to get data about a list of account numbers in an Access table from SQL Server based on the account number field which is common to both tables. Realizing that I can create a persistent temp table on SQL Server, I used a combination of DAO and ADO to get the values from the Access table and create a temp table. All I had to do then is run the pass-through query which references the temp table. The only odd thing (which is not a problem at this point) is if I create the temp table and run the pass-through query in VBA, this setup works. But if I create the temp table in VBA and double-click on the pass-through query, Access says that the temp table can't be found. Anyway, here's the code:
Public Sub Insert_Into_Access_From_ADO_Recordset_Using_PTQ_Simpler()
Dim dbs As DAO.Database
Set dbs = CurrentDb()
Dim cnn As ADODB.Connection
Set cnn = New ADODB.Connection
Dim rst As ADODB.Recordset
'Open SQL Server
Dim str_cnn As String
str_cnn = "MYDSN"
cnn.Open str_cnn
' Drop the temp table:
Dim str_SQL_Drop_Temp_Table As String
str_SQL_Drop_Temp_Table = "IF OBJECT_ID('tempdb..##BumpData','U') IS NOT NULL "
str_SQL_Drop_Temp_Table = str_SQL_Drop_Temp_Table & " DROP TABLE ##BumpData "
cnn.Execute str_SQL_Drop_Temp_Table
' Create the temp table:
Dim str_SQL_Create_Temp_Table As String
str_SQL_Create_Temp_Table = " CREATE TABLE ##BumpData "
str_SQL_Create_Temp_Table = str_SQL_Create_Temp_Table & " " & "("
str_SQL_Create_Temp_Table = str_SQL_Create_Temp_Table & " " & " ID INT "
str_SQL_Create_Temp_Table = str_SQL_Create_Temp_Table & " " & " , AccountNumber VARCHAR(Max)"
str_SQL_Create_Temp_Table = str_SQL_Create_Temp_Table & " " & ")"
cnn.Execute str_SQL_Create_Temp_Table
' Insert values from the Access table into the temp table
' by looping through the Access table as a recordset:
Dim rst_DAO As DAO.Recordset
Set rst_DAO = dbs.OpenRecordset("Bump Data")
Dim str_SQL_Insert As String
rst_DAO.MoveFirst
With rst_DAO
Do While Not rst_DAO.EOF
'str_Loan_Number_List = str_Loan_Number_List & "'" & Trim(rst![Loan Number]) & "'" & ","
str_SQL_Insert = " INSERT INTO ##BumpData VALUES (" & rst_DAO![ID] & ",'" & Trim(rst_DAO![Loan Number]) & "') "
cnn.Execute str_SQL_Insert
.MoveNext
Loop
End With
' Run the pass-thru query which joins to the temp table:
DoCmd.SetWarnings False
DoCmd.RunSQL "SELECT * INTO [Bump Results] FROM [Bump PTQ]"
DoCmd.SetWarnings True
End Sub
You reported this line triggers the error:
Set ObjRecordset3 = "SELECT * FROM " & Access_Recordset
The right side of the = sign attempts to concatenate a string with an ADODB.Recordset object. That is probably the immediate cause of the compile error. However, that's not the only problem with that line. Set <recordset object> = "any string value" will not work. And finally, Access SQL does not support FROM <recordset object> for any type of recordset object (ADO or DAO).
I think you should look for a simpler approach. In following query, dbo_BigIntTable is an ODBC link to a SQL Server table. It includes a field, bigint_fld, whose SQL Server data type is BigInt. However, Access sees that field as Text type. Therefore I can join it with the string equivalent of a Long Integer field (tblFoo.id).
SELECT tblFoo.id, dbo_BigIntTable.bigint_fld
FROM
tblFoo
INNER JOIN dbo_BigIntTable
ON CStr(tblFoo.id) = dbo_BigIntTable.bigint_fld;
The Access query designer complained it can't display that join in Design View, but I was able create the join from SQL View and it worked fine.