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.
Related
I am currently trying to write a select pass through query using VBA in Access 2016. If I use the manual option via the button Pass-Through and assign manually the dsn the following statement works.
SELECT top 1 dat_Kunden.Kunden_Status FROM dat_Kunden
The sql I want to pass through is changing so I want to create a VBA Function to execute it.
This is my current Function to execute a given sql statement
Function CreateSPT(strSQL As String)
Dim qdf As DAO.QueryDef, rs As DAO.Recordset
Set qdf = CurrentDb.CreateQueryDef("")
qdf.Connect = "ODBC;Driver=SQL Server;SERVER=xxx;DATABASE=yyy;UID=zzz" 'in the code this is the real data
qdf.SQL = strSQL
qdf.ReturnsRecords = True
Set rs = qdf.OpenRecordset()
If Not (rs.BOF And rs.EOF) Then rs.MoveFirst
Do Until rs.EOF
Debug.Print rs.Fields(0)
rs.MoveNext
Loop
rs.Close
Set rs = Nothing
Set qdf = Nothing
End Function
This does work.
Sub test_sql()
SQL = "SELECT CONVERT( date, GETDATE() ) AS qryTest"
CreateSPT (SQL)
End Sub
This statement which works via the manual pass through does not work
Sub test_sql2()
SQL = "SELECT top 1 dat_Kunden.Kunden_Status FROM dat_Kunden AS qryTest"
CreateSPT (SQL)
End Sub
The Error code is Run-time error '3146': ODBC -- call failed at this line:
Set rs = qdf.OpenRecordset()
I hope you have an idea where my mistake is... Thanks to all of you, learned a lot from you!
If you provide an alias, use it:
SQL = "SELECT Top 1 qryTest.Kunden_Status FROM dat_Kunden AS qryTest"
or ignore it:
SQL = "SELECT Top 1 Kunden_Status FROM dat_Kunden AS qryTest"
I have been struggling with getting this code to work for a few days. If you could offer any solutions I would really appreciate it.
Private Sub Command0_Click()
If IsNull(NewSupBox.Value) Or IsNull(NewNumberBox.Value) Then
MsgBox ("All fields must be filled")
GoTo ErrorExit
End If
If Not IsNull(DLookup("SupplierNumber", "SupGenInfo ", "SupGenInfo.SupplierNumber =" & NewSupBox)) = Then
MsgBox ("This supplier number already exists. You can edit the current record on the Edit supplier page.")
GoTo ErrorExit
End If
Dim db As Database
Dim rec As Recordset
Set db = CurrentDb
Set rec = db.OpenRecordset("select * from SupGenInfo")
rec.AddNew
rec("SupplierNumber") = Me.NewSupBox.Value
rec("SupplierName") = Me.NewNameBox.Value
rec.Update
Set rec = Nothing
Set db = Nothing
MsgBox "Records added successfully."
ErrorExit:
End Sub
Edit: Forgot to mention that I am not getting any error message. The command will simply not add a new record to my table.
Edit2: The code above will output the msg "Records Added Successfully" when i remove the following block of code.
Dim db As Database
Dim rec As Recordset
Set db = CurrentDb
Set rec = db.OpenRecordset("SupGenInfo")
rec.AddNew
rec("SupplierNumber") = Me.NewSupBox
rec("SupplierName") = Me.NewNameBox
rec.Update
Set rec = Nothing
Set db = Nothing
It is when this code is included that my command click becomes unresponsive.
I believe, you are reading a table (for display purposes) with your select * ... statement, then you're adding a new record to that list rather than the actual database. When you open OpenRecordset, just supply the table name, not a whole SQL query shebang...
I created a new table, so edit this code to match your parameters/values, otherwise this has been tested to work:
Dim db As Database
Dim rec As Recordset
Set db = CurrentDb
Set rec = db.OpenRecordset("Table1")
rec.AddNew
rec("Field1") = 1234
rec("Field2") = "blah2"
rec("Field3") = "blah3"
rec.Update
Set rec = Nothing
Set db = Nothing
Hope this helps.
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.
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.
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.