run time error 3061 - ms access - vba

I am relatively new to MS Access and VBA. I am trying to do a bit of code for this DB and get error message 'Run Time Error 3061. Too Few parameters. Expected 1" when it gets to the OpenRecordSet clause.
I have been researching and looking at this for days but cannot figure out the reason for the error. I know the error is in the SELECT specifically in the WHERE clause when the form is being closed.
The strange thing is that when i change the condition to Record_Num = 2 or any specific number it seems to work, but i need to use the Record_Match_Temp variable.
Any help will be appreciated. Thanks in advance. Here is the code
Option Compare Database
Dim Record_Match_Temp As Integer
Dim Logged_Now As String
Private Sub Form_Close()
Dim db2 As Database
Dim rs2 As Recordset2
Dim SelStr As String
Set db2 = CurrentDb()
SelStr = "SELECT Record_Num FROM User_Log WHERE Record_Num = Record_Match_Temp"
Set rs2 = db2.OpenRecordset(SelStr)
End Sub
Private Sub Form_Load()
Form_User_Name = Environ("UserName")
Logged_Now = Now()
Dim db As Database
Dim rs As Recordset
Set db = CurrentDb()
Set rs = db.OpenRecordset("Select * from [User_Log]")
rs.AddNew
rs![Log_User_Name] = Environ("UserName")
rs![Logged_Computer] = Environ("ComputerName")
rs![Logged_In] = Logged_Now
rs![Record_Match] = rs![Record_Num]
Record_Match_Temp = rs![Record_Num]
' rs![Logged_Out] = Now()
rs.Update
End Sub
Private Sub Form_Timer()
Date_Time.Requery
End Sub

SelStr = "SELECT Record_Num FROM User_Log WHERE Record_Num = " & _
Record_Match_Temp
Add single quotes around Record_Match_Temp if the field is not numeric.

Related

VBA LOOP in ORACLE SQL

I was trying to write a VBA to run a query assigned to MySQL only when the HCR_DM.HCR_DM_FACT table is fully loaded. I'm using the count of distinct source in that table to decide if it is fully loaded.
When I was running the Macro below, I got an error message for the Do While line, saying that Object doesn't support this property or method.
I'm quite new to VBA, and I couldn't figure out what need to be adjusted. Can some one help me with this?
Thanks!
Const CNSTR = "Provider = OraOLEDB.Oracle; Data Source =CSDPRO; ODBC;DRIVER={Oracle ODBC Driver};SERVER=CSDPRO;User ID=HCR_SANDBOX;password=******"
Sub FillWithSQLData(strSQL As String, wkSht As String)
' Given a SQL query and worksheet, fills the worksheet with a data dump of the SQL query results
' Define variables
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim sql_count As String
' Set variables
Set cn = New ADODB.Connection
Set rs = New ADODB.Recordset
' Connect to SQL Server
With cn
.ConnectionString = CNSTR
.Open
End With
' Query SQL Server and return results as a data dump in cell A2 of the specified worksheet
With rs
.ActiveConnection = cn
sql_count = "select count( distinct a.src_table) from hcr_dm.hcr_dm_fact a"
Set rf = cn.Execute(sql_count)
Do While rf.Fields.Value = 8
.Open strSQL
Loop
Worksheets(wkSht).Range("A2").CopyFromRecordset rs
.Close
End With
' Close connection
cn.Close
Set rs = Nothing
Set Conn = Nothing
End Sub
Sub Refresh()
' Define SQL query
Dim mySQL As String
mySQL = "select a.oracle_fin_company_id || ' - ' || a.oracle_fin_company_desc as COMPANY " & _
"From hcr_dm.legal_entity_summary a " & _
"where a.Company_Header = 'Filed'"
' Choose worksheet where results should be displayed
Dim myWkSht As String
myWkSht = "Sheet1"
' Call connection sub
Call FillWithSQLData(mySQL, myWkSht)
End Sub
You're not selecting a field. The lines
Set rf = cn.Execute(sql_count)
Do While rf.Fields.Value = 8
Should probably be
Set rs = cn.Execute(sql_count)
Do While rs.Fields(0).Value = 8
Also, note the typo in that you declared rs but you're filling rf with the Recordset.
I recommend you use the Option Explicit statement to help find these. You can read more about it here.

How to execute an SQL statement in VBA using " sign to Get the ID with RecordSet Access 2013

I am looking to execute the following code, the problem is, I need to put a String variable in the SQL which should use the "" sign inside the SQL statement.
The problem is, I cannot use multiple "". I tried using Char to use the sign " and convert it to a String, and that's when I got lost.
The code below shows 2 Functions. 1 To see if I get it right to print the correct SQL statement, the other is a Function to return the actual record ID which I will then use to add a new record.
What I mainly looking to do, is use the RecordSet to get a record ID of one of the records.
Any help would be highly appreciated.
Private Sub PrintSQLStatement_Click()
Dim strSQL As String
Dim strTmp As String
Dim i As Integer
strTmp = "UE243"
strSQL = "SELECT tbl_Sales.[ID], tbl_Sales.[SaleReference] FROM tbl_Sales WHERE (((tbl_Sales.[SaleReference])= "" + strTmp + ""));"
i = MsgBox(strSQL, vbOKOnly)
End Sub
Private Function GetSaleID(ByVal strSaleRef As String) As Integer
Dim db As Database
Dim rs As Recordset
Dim strSQL As String
Dim ID As Integer
strSQL = "SELECT tbl_Sales.[ID], tbl_Sales.[SaleReference] FROM tbl_Sales WHERE (((tbl_Sales.[SaleReference])= "" + strSaleRef + ""));"
Set db = CurrentDb
Set rs = db.OpenRecordset(strSQL)
ID = rs.GetID
rs.Close
Set rs = Nothing
db.Close
GetSaleID = ID
End Function
You can avoid character escaping issues by using a QueryDef object to run a parameterized query like so:
Private Function GetSaleID(ByVal strSaleRef As String) As Long
Dim db As DAO.Database
Dim qd As DAO.QueryDef
Dim rs As DAO.Recordset
Dim strSQL As String
Dim ID As Long
strSQL = _
"PARAMETERS prmSaleRef TEXT(255);" & _
"SELECT tbl_Sales.[ID] FROM tbl_Sales " & _
"WHERE tbl_Sales.[SaleReference]=[prmSaleRef]"
Set db = CurrentDb
Set qd = db.CreateQueryDef("", strSQL)
qd!prmSaleRef = strSaleRef
Set rs = qd.OpenRecordset(dbOpenSnapshot)
ID = rs!ID
rs.Close
Set rs = Nothing
Set qd = Nothing
db.Close
Set db = Nothing
GetSaleID = ID
End Function

MS Access Error 3622 vba

I have an Access 2010 database that was local and I have since linked to a SQL 2012 database. However, I have a form to insert a highlight record that runs the below code:
Private Sub Command18_Click()
Dim R As Recordset
Set R = CurrentDb.OpenRecordset("SELECT * FROM [tblJobHead]")
R.AddNew
R![Rep Num] = [Forms]![frmMain]![NavigationSubform].[Form]![RepNum]
R![Item Number] = Me.ItemNumber.Value
R![Description] = Me.Desc.Value
R![EmpID] = [TempVars]![EmpID]
R![Status] = 2
R.Update
R.Close
Set R = Nothing
DoCmd.Save
End Sub
However, when I click the button I now receive the error:
Error 3622 - You must use the dbSeeChanges option with OpenRecordset when accessing a SQL Server table that has an IDENTITY column
Any ideas?
Regards,
Michael
The error is exactly as it says, try:
Function GetRecordset(sSQL) As DAO.Recordset
Dim rdao As DAO.Recordset
Dim db As Database
Set db = CurrentDb
Set rdao = db.OpenRecordset(sSQL, dbOpenDynaset, _
dbFailOnError + dbSeeChanges)
If Not rdao.EOF Then
rdao.MoveLast
rdao.MoveFirst
End If
Set GetRecordset = rdao
End Function
I would strongly advise you to avoid single letters as variables.

Populate field in ms access form from field in ado recordset record of the same name

I have a form in Access that I am trying to populate during it's Current event using the fields from an ADO Recordset. I am using Sql Server for the database and am using the recordset to try to populate to corresponding fields on the form with what is in the recordset. Right now it works like this:
Private Sub Form_Open(Cancel As Integer)
On Error GoTo Err_FormOpen
Set rst As new ADODB.Recordset
With rst
.ActiveConnection = CurrentProject.AccessConnection
.CursorType = adOpenKeyset
.LockType = adLockOptimistic
.Source = "SELECT * FROM [Outage Summary] ORDER BY OutageSummaryID"
.Open
End With
Set Me.Recordset = rst
End Sub
Private Sub Form_Current()
OutageSummaryID.Value = Me.Recordset!OutageSummaryID
Dispatcher.Value = Me.Recordset!Dispatcher
StartDateTime.Value = Me.Recordset!StartDateTime
Location.Value = Me.Recordset!Location
CityRelated.Value = Me.Recordset!CityRelated
Scheduled.Value = Me.Recordset!Scheduled
CustomerEquip.Value = Me.Recordset!CustomerEquip
DispatchBusHrs.Value = Me.Recordset!DispatchBusHrs
TotalCount.Value = Me.Recordset!TotalCount
Substation.Value = Me.Recordset!Substation
CompletionDateTime.Value = Me.Recordset!CompletionDateTime
CustCallDateTime.Value = Me.Recordset!CustCallDateTime
FirstRespDateTime.Value = Me.Recordset!FirstRespDateTime
Feeder.Value = Me.Recordset!Feeder
SwitchingSchedule.Value = Me.Recordset!SwitchingSchedule
Cause.Value = Me.Recordset!Cause
ActionTaken.Value = Me.Recordset!ActionTaken
Me.ME.Value = Me.Recordset!ME
MI.Value = Me.Recordset!MI
End Sub
But I would like the current subroutine to work something like this:
Dim fld As ADODB.Field
Dim nam As String
For Each fld In Me.Recordset.Fields
Debug.Print fld
nam = fld.Name
Me.Controls(nam).Value = fld.Value
Next
With the code as it stands I am getting an error "The recordset is not updatable"
Thanks for any help!
This is because you are not binding to the recordset. It would be better if you stored your query and attached it to the form as the recordsource so that way it will bind it to the form. Then you set the record source of each field in design mode and no code is needed and it will be updateable depending on your permission to the SQL Server.

Errors with linked tables and Ms Access ( Run-time error '3622' : dbSeeChanges/Identity column )

I am trying to output the name of all linked tables, including their fields which are Date/Time, and that fields values.
The following code can output the first table, field name and their first value, not all values, although when it gets to the next linked table, I get this error
Run-time Error '3622'
You must use the dbSeeChanges option with OpenRecordSet when accessing a SQL Server table that has an IDENTITY column.
Here is my code
Private Sub btnGetFields_Click()
Dim db As DAO.Database
Dim tdf As DAO.TableDef
Dim f As Field
Dim rst As DAO.Recordset
Dim numField As Integer
Set db = CurrentDb
For Each tdf In db.TableDefs
If Left$(tdf.Connect, 9) = "ODBC;DSN=" Then
Set rst = CurrentDb.OpenRecordset(tdf.Name)
numField = rst.Fields.Count
Debug.Print "Table: " & tdf.Name
For index = 0 To numField - 1
If rst.Fields(index).Type = dbDate Then
Debug.Print "Field: " & rst.Fields(index).Name; " Value : "; rst.Fields(index).Value
End If
Next
End If
Next
Set tdf = Nothing
Set db = Nothing
End Sub
I read something that if I'm using sql tables I should use ADO?
Any ideas?
You can continue to use your existing DAO code, just change
Set rst = CurrentDb.OpenRecordset(tdf.Name)
to
Set rst = CurrentDb.OpenRecordset(tdf.Name, dbOpenSnapshot)
That opens a static read-only Recordset, so dbSeeChanges is not required.