Using the Month function as criteria in an Access SQL query - sql

I'm trying to extract data from an Access 2016 database using SQL and VBA. Below is the code I'm trying to use and every time I run it, I get a "No value given for one or more parameters". I've also shown what I see in the immediate window.
vsql = "SELECT [ResDate],[ResNanme],[ResStart],[ResEnd] FROM [TrainingRoom] where Month([ResDate]) = " & MonNo
Set RecSet1 = Connection.Execute(vsql, dbrows, adCmdText)
Immediate Window:
SELECT [ResDate],[ResNanme],[ResStart],[ResEnd] FROM [TrainingRoom] where(Month([ResDate])) = 11
I don't see anything wrong but I'm sure this is user error. The "MonNo" variable is declared as an integer.
Any suggestions would be greatly appreciated. Thanks for the help.....

I've never used Execute method to open ADO recordset, only Open.
Assuming Connection is a declared and set variable - but it is a reserved word and really should not use as a variable. If code is behind same db pulling data from, could just use CurrentDb with DAO recordset variable and OpenRecordset method. Example that accepts default parameters for optional arguments:
Dim RecSet1 As DAO.Recordset
Set RecSet1 = CurrentDb.OpenRecordset(vsql)

Related

Passing a SQL result as an argument for a VBScript object

I am working on an extant .Asp program at the moment which contains the following snippet:
Dim strRel_Report_Location
Dim rs, sql, blnReportExists
Dim strUID
Dim intMonth, intYear
sql = "Select NewID() AS UID"
Set rs = DB.Execute(Sql)
strUID = rs("UID")
strRel_Report_Location = "/Admin/Reports/Temp/t" & strUID & ".html"
My question is, how is it possible to pass the string "UID" as an argument to the object 'rs'? One may easily divine from the code that the intent is to pass the resulting "NewID()" call to the path which is assigned in the statement:
strRel_Report_Location = "/Admin/Reports/Temp/t" & strUID & ".html"
Any illumination would be greatly appreciated as I don't wish to just make assumptions on the code with which I am working.
I'm sure this is a layup for you guys, but thank you for your help!
I have to make an assumption here because you never show how the DB variable is created. I am going to assume you are working with straight ADO because the call signatures in your code match that.
Again, you are probably working with an Ado Command instance which has the method Execute. This method takes a sql string, executes it on the database server, and then returns a Recordset which contains the results of the query that executed. The recordset has an indexer with the name of the columns, using that indexer you can get the value in the column.
If you want to learn more about this code it would help to start reading up on how to work with ADO.

Calling scalar UDF function from ms access

I am trying to call a scalar function from my database in ms access.
This function should return one row of my salted and hashed parameters.
My initial attemt looked like this and returned the following error: "undefined function 'dbo.SaltAndHashPassword' in expression"
Dim rs As Recordset
Set rs = CurrentDb.OpenRecordset("SELECT dbo.SaltAndHashPassword('" & Me.txtWW & "','" & Me.salt & "') as salted", dbOpenSnapshot, dbReadOnly)
I tried the solution from this question but I got the error "object required"
I also tried the solution from this question but I can't get it to work(probably due to wrong parameters)
Can anybody help me with this problem?
Thanks in advance.
You can't use CurrentDbin that case. You have to set a Connection object to the server/db where the dbo.SaltAndHashPassword function is stored (the example you mention uses a connection, even if the code to set it up is not shown), since it is totally unknown from Access.
Alternatively you can create a passthru query in Access and use DAO. I find it easier and it avoids creating yet another external reference.
Something like:
Set db = CurrentDb()
'Create a temporary passthrough query '
Set ptq = db.CreateQueryDef("")
'set ODBC connection '
ptq.Connect = "your connect string"
ptq.SQL = "SELECT * from your SQL"
ptq.ReturnsRecords = True
Set rs = ptq.OpenRecordset(dbOpenSnapshot)

SELECT INTO in VBA results in no records selected or written

When I use this SQL in Access, it works fine. In VBA, the result is to create a new table with no records. It must be a dumb mistake on my part but can't find it. Many thanks for any help or point in right direction.
Mike
strSQL = "SELECT tempconsoltb.[sub-accountNumber],tempconsoltb.[sub-accountDescription], tempconsoltb.sumOfCMBaseCurr INTO tblTempConsolTB FROM tempConsolTB;"
Set qdf = CurrentDb.CreateQueryDef("", strSQL)
qdf.Parameters(0) = Forms!frmMain.txtPM
qdf.Parameters(1) = Forms!frmMain.txtPY
qdf.Parameters(2) = Forms!frmMain.cboBaseFX
qdf.Parameters(3) = Forms!frmMain.cboConsolCo
qdf.Execute
Try checking that the objects in your forms are named properly and are actually feeding the values into the query. You can always use the Watch function in your compiler to see the values there or create a string variable that you initialize its value within the code to see what your actual query and put a watch on it when running the code.

Current Recordset does not support bookmarks

I have this ASP classic code that will return a set of records (recordset):
adoRs.Open "EXEC SP_SelectFromTable", adocn, 1
Its obviously from a Stored Procedure source. Now I use a AbsolutePage property for pagination function but it cause an error:
Error Type: ADODB.Recordset (0x800A0CB3) Current Recordset does not
support bookmarks. This may be a limitation of the provider or of the
selected cursortype.
But when I changed it to a simple select statement like below. It work just fine.
adoRs.Open "SELECT * FROM tblSample", adocn, 1
Any concept I'm missing?
When I first started working with ADO in ASP I ad the same problem. Most of the easy to find documentation mentions setting the cursor type of the recordset object. But on our servers, I actually have to set it on my connection object to get it to work (never really figured out why).
So on my applications I set it on my connection object like this:
adocn.CursorLocation = adUseClient
Then I can set my recordset as:
adoRs.CursorType = adOpenStatic
Once the recordset is opened the datatype of Bookmark will be changed to either Double or Integer according to the recordcount. Just assign the record number in the variable of that type then it will work fine...
Note:-To know type of bookmark use typename(rs.bookmark) after recordset is opened

How to run a SQL select statement in VB

I have been looking around but can't seem to find out how to do this.
I'm trying to execute a SELECT sql statement in VB that takes in a parameter from a form. For example the user selects a From and a To date and the SQL should take what they selected.
I know that you cannot use the DoCmd.Execute method for SELECT SQL so I would need to open the record set - but how? This is what I have tried so far
Dim recordSet As DAO.recordSet
Dim SQL As String
SQL = "SELECT * FROM tblWebMeetingData"
Set recordSet = CurrentDb.OpenRecordset(SQL)
'More code in here
recordSet.Close
Set recordSet = Nothing
Because this executes without an error I assume it's actually getting the results - so is there a way to see what it is actually returning?
Thanks
First: It's a good advice to rename the recordset to rs, for example, because "recordset" is a reserved name. This is misunderstandable.
This recordset contains the records you queried by your SQL statement. You may access those data by rs.fields("fieldname").value. Move to the next recordset with rs.movenext.
To incorporate the form's control value I use the way to build the full SQL statement prior to opening the recordset. Say the form is named "myform" and the control "mycontrol", you may write some kind of
SQL = "SELECT * FROM tblWebMeetingData WHERE myfield = " & forms!myform.mycontrol.value
Please be sure the form only contains valid values, because any wrong formatted value will directly lead to an SQL execution error.
I hope it was this, what you wanted.
Here you have come sample code about iterating trought RecordSet and using values from it( I hope it helps a bit):
Dim i As Integer
Do While Not rs.EOF
Sheets("D_" & day).Cells(i, 1) = rs.Fields(0).Value
Sheets("D_" & day).Cells(i, 2) = rs.Fields(1).Value
rs.MoveNext
i = i + 1
Loop
rs.Close