Error while running Docmd runsql using variable - ms-access

I know i am asking an easy question,
I Wasn't able to find the cause for the Problem.
When i run the followng code it gives me error in Docmd RunSqL Command showing invalid arguement. It seems that it could be due to variable ListTablename.
Here is my Code
Function RefreshedLinks()
Dim rs1 As DAO.Recordset
Dim rs2 As DAO.Recordset
Dim stringsql As String
Dim strPath As String
Dim Foldername As String
Dim strTableNameCheck
Dim obj As AccessObject, dbs As Object
Set dbs = Application.CurrentData
Set rs1 = CurrentDb.OpenRecordset("Table Valued Parameter")
ListTablename = "SELECT [LE Based Table].[Table Name], [LE Based Table].LE FROM [LE Based Table] WHERE ((([LE Based Table].LE)=[Forms]![Combo Box]![LE Select]));"
DoCmd.SetWarnings False
DoCmd.RunSQL ListTablename
DoCmd.SetWarnings True
End Function
The where condition of query is a string function based on selection done by user on the form combo box like XLL,XLI
Is there something wrong with my query?
Thanks in Advance

In this case why you don't just insert values into SQL:
ListTablename = "SELECT [LE Based Table].[Table Name], [LE Based Table].LE FROM [LE Based Table]
WHERE ((([LE Based Table].LE)=" & [Forms]![Combo Box]![LE Select] & "));"
Sorry, but RunSQL could be used for an action query or a data-definition query. So create query, save it to db and then use DoCmd.OpenQuery

Related

Using SQL and VBA in combination

Okay I'm new here and will be my first question, I'm getting stuck with my code that I'm busy with.
I keep bumping same error which is related to the Runtime error: 3129 in Access Visual Basic
Now it is an importing button function set in a Public Sub check below my code:
Public Sub ImportDataTablebtn_Click()
Dim strSQL As String
Dim db As DAO.Database
Dim tbl1 As DAO.TableDef
Dim tbl2 As DAO.TableDef
Dim fld1tbl1 As DAO.Field
Dim fld2tbl1 As DAO.Field
Dim fld3tbl1 As DAO.Field
Dim fld1tbl2 As DAO.Field
Dim fld2tbl2 As DAO.Field
Dim fld3tbl2 As DAO.Field
Set db = CurrentDb
Set tbl1 = db.TableDefs("Clicks Returns")
Set tbl2 = db.TableDefs("Oct 2015 Clicks Returns")
Set fld1tbl1 = tbl1.Fields("SKU")
Set fld2tbl1 = tbl1.Fields("Item Description")
Set fld3tbl1 = tbl1.Fields("Oct 2015 FIN YTD TY % Returns")
Set fld1tbl2 = tbl2.Fields("Sku")
Set fld2tbl2 = tbl2.Fields("Item Description")
Set fld3tbl2 = tbl2.Fields("F21")
DoCmd.RunSQL strSQL
strSQL = "INSERT INTO tbl1 (fld1tbl1, fld2tbl1, fld3tbl1)" & _
"SELECT fld1tbl2, fld2tbl2 fld3tbl2 FROM tbl2;"
Set db = Nothing
End Sub
Now I know some of my lines like the table rename and field might not really be required, though I thought that it would just make it easier with the code...
Please let me know where I'm making my error as I feel like going crazy by keep looking at it over and over.
In the database is the tables that I'm referring towards and I want to move the certain fields from the one table to the other table as it is imported excel sheets, I'm busy making it an automated system where someone would just click on buttons on a form and it will sort out the data.
Thank you for taking the time to read this essay and also answering it
There are a couple of mistakes in your code.
First the code will not substitute your variables for the table/field names. So the string SELECT fld1tbl2 is not converted to SELECT [Item Description].
Second you are trying to run your query before you have built it.
Finally Access uses VBA and not VBScript. It's a different language. While they have much in common there are some important differences. Stick to the the VBA tag and you will be more likely to get the answers you need.
I've refactored your code. This version builds and then runs the query.
Public Sub ImportDataTablebtn_Click()
Dim strSQL As String
' Build Query.
strSQL = "INSERT INTO [Clicks Returns] " & _
"(SKU, [Item Description], [Oct 2015 FIN YTD TY % Returns]) " & _
"SELECT Sku, [Item Description], F21 FROM [Oct 2015 Clicks Returns];"
' Run Query.
DoCmd.RunSQL strSQL
End Sub

3061 VBA Error - SQL Query "Too few parameters. Expected 1." Simple query

I have the following code:
Private Sub lst1Model_Operation_Click()
Dim db As Database
Dim sSQL As String
Dim rst As Recordset
Set db = CurrentDb
sSQL = "SELECT * FROM qryOrder_Model_Operation_Value WHERE Model_Operation_ID = " & CInt(Me![lst1Model_Operation].Value)
Debug.Print sSQL 'when pasted this into a query SQL, it works flawlessly.
Set rst = db.OpenRecordset(sSQL) 'error line
'some code here
rst.Close
Set db = Nothing
End Sub
I'm at loss at what to do. The Debug.Print looks like:
SELECT * FROM qryOrder_Model_Operation_Value WHERE Model_Operation_ID = 748
And as I said, if I paste that Debug.Print into a Query in the Access itself, it produces the desired results.
I have tried adding ' ' around the value, but using CInt() I already made sure it is parsed as integer. The Model_Operation_ID also expects to get an integer (otherwise it wouldn't work in a separate query either).
Edit:
The qryOder_Model_Operation_Value is as follows:
SELECT tbl1Model_Operation.Model_Operation_ID, tbl1Model_Operation.Model_ID, tbl1Model_Operation.Operation_Value_ID, tbl2Operation_Value.Operation_Name_ID, tbl3OperationsList.Operation_Name, tbl1Order_Model.Quantity AS [Počet párov], tbl1Order_Model.Order_ID
FROM tbl3OperationsList INNER JOIN (tbl2Operation_Value INNER JOIN (tbl1Model_Operation INNER JOIN tbl1Order_Model ON tbl1Model_Operation.Model_ID = tbl1Order_Model.Model_ID) ON tbl2Operation_Value.Operation_Value_ID = tbl1Model_Operation.Operation_Value_ID) ON tbl3OperationsList.Operation_ID = tbl2Operation_Value.Operation_Name_ID;
Make sure Access understands you want rst to be a DAO.Recordset instead of an ADODB.Recordset:
'Dim rst As Recordset
Dim rst As DAO.Recordset
Both the ADO and DAO object models include Recordset objects. Although similar in some respects, they can not be used interchangeably.
When your Access VBA project references include a version of ADO (ActiveX Data Objects) and that reference has a higher priority than the DAO reference, Dim rst As Recordset will give you an ADODB.Recordset. Avoid unwelcome surprises by always qualifying your Recordset declarations with DAO or ADODB as appropriate.
Try this way:
Set rst = db.OpenRecordset(sSQL,dbOpenTable)
or
Set rst = db.OpenRecordset(sSQL,dbOpenSnapshot)

Setting a query to a recordset

I am trying to set a recordset that keeps giving me the error message - "Runtime Error 3061 Too few parameters. Expected 1".
The query checks to see what value is in a combo box and then returns two fields. This is the VBA code I tried -
Dim db As Database
Dim rst As Recordset2
Dim field As field
Dim n As Integer
Dim Qno As Integer
Dim sqlstr As String
Set db = DBEngine(0)(0)
Set rst = db.OpenRecordset("Get_Questions", dbOpenDynaset)
This is the SQL behind the Get_Questions Query -
SELECT Question_Lt.Qnumber, Question_Lt.Questions, Question_Lt.Freq
FROM Question_Lt
WHERE (((Question_Lt.ClientCd)=[Forms]![TestForm]![CmClient]));
Use your query as a QueryDef object, supply the parameter value, and then use its OpenRecordset method to populate your recordset.
'Set rst = db.OpenRecordset("Get_Questions", dbOpenDynaset)
Dim qdf As DAO.QueryDef
Set qdf = db.QueryDefs("Get_Questions")
qdf.Parameters(0).Value = [Forms]![TestForm]![CmClient]
Set rst = qdf.OpenRecordset(dbOpenDynaset)
i ve to few reputation points to comment your question. But what ist the SQL Statement exactly. It seems to me that your ' ' are not set but i don' t know
Im Just guessing: Sql Script for Get_Questions should be something like that. I don' t know the parameter "Get_Questions" is it a query-name?
sqlStr =
"SELECT...
FROM Question_LT
WHERE Client.Cd = " & [Forms]![TestForm]![CmClient]

Advanced Filter Criteria in Access

The issue is simple but I just cant figure it out.
I have two tables in access, one with records and another with "key words". I need to filter the records containing certain "key words". In other words, use one table field as a filter criteria for the other, but without linking them because the "key words" table just contains random words instead of a whole record.
In excel I can run an advanced filter on my records and just specify as criteria the list of key words (and using wildcards), but in acces I havent found a way to filter according to another table fields.
Any ideas about it?
You may need to create a function that spits out custom SQL with all the keywords in it. Here is an example to get you started.
Public Function fGetTrashRecords()
'add your own error handling
Dim SQL As String
Dim rst As DAO.Recordset
Dim rstTrash As DAO.Recordset
Dim db As DAO.Database
Set db = CurrentDb
Set rst = db.OpenRecordset("SELECT sKeyWord FROM tblBadKeyWords", dbOpenSnapshot)
If Not rst Is Nothing Then
rst.MoveFirst
Do While Not rst.EOF
SQL = SQL & " strFieldContaingKeyWord LIKE *'" & rst!sKeyWord & "'* OR"
rst.MoveNext
Loop
If SQL > "" Then SQL = Left(SQL, Len(SQL) - 2) 'get rid of the last OR
rst.Close
Set rst = Nothing
End If
If SQL > "" Then
Set rstTrash = db.OpenRecordset("SELECT * FROM tblHasKeyWords WHERE " & SQL, dbOpenDynaset, dbSeeChanges)
If Not rstTrash Is Nothing Then
rstTrash.MoveFirst
Do While Not rstTrash.EOF
Debug.Print rstTrash!ID
rstTrash.MoveNext
Loop
rstTrash.Close
Set rstTrash = Nothing
End If
End If
Set db = Nothing
End Function

how to read one field from one record

I know I'm over thinking this, but I want to check a single value/field within a single record. For instance, I want to know if the value of the "closedDate" field in the record with the primary key of 33 is null or not.
I was thinking something like:
dim db as DAO.Database
dim rs as DAO.Recordset
set db = CurrentDb
set rs = db.OpenRecordset("record_holdData")
If not isNull(rs.Fields("closedDate")) then
'do nothing
Else
'add a close date
End If
But I don't think this is right. It doesn't specify the record number. In the application, the form is opened by being bound to the record in question, but I don't think CurrentDb takes that into consideration and rather references the entire table.
So my question is, how do open the recordset in this fashion and reference this field in that particular record only?
You found the answer you wanted, but I would use the DLookup Function instead.
Dim db As DAO.Database
Dim strWhere As String
Dim varClosedDate As Variant
Set db = CurrentDb
strWhere = "id = 33"
varClosedDate = DLookup("closedDate","record_holdData",strWhere)
If IsNull(varClosedDate) = True Then
'use today's date as closedDate
db.Execute "UPDATE record_holdData Set closedDate = Date() WHERE " & strWhere
End If
I was under the impression that the argument for the .OpenRecordset() method was to be a table name only. But it turns out you can send it a query too:
set rs = db.OpenRecordset("select * from record_holdData where id = 33")