Link to home
Start Free TrialLog in
Avatar of stang1
stang1

asked on

VBA update SQL Server tbl

New to VBA....  I am trying to make an update to a linked SQL Server table.  Thus far it can't seem to identify the table: "Variable not defined".  Do you see any syntax issues with the attached code?
I have a similar form and it finds and inserts a rec into the same tbl....
Thanks!
Dim db As dao.Database
   Dim rs As dao.Recordset
   Dim frm As Form
   
      
Set frm = Forms!PutBoxOnHold
Set db = CurrentDb
   BoxID = frm.BoxID
   
   
Set rs = db.OpenRecordset("dbo_BoxInformation", dbOpenDynaset, dbSeeChanges)
 
With rs
   If gbl_hold_box = 1 Then
      DoCmd.RunSQL "UPDATE BoxInformation SET BoxInformation.BoxStatusID = 3"
                   WHERE BoxInformation.BoxID = " & Forms![PutBoxOnHold]![BoxID] & ""';"
   ElseIf gbl_go_box = 2 Then
      DoCmd.RunSQL "UPDATE BoxInformation SET BoxInformation.BoxStatusID = 4"
                   WHERE BoxInformation.BoxID = " & Forms![PutBoxOnHold]![BoxID] & ""';"
  End If
End With
rs.Close
Set rs = Nothing
Set db = Nothing
End Sub

Open in new window

Avatar of reb73
reb73
Flag of Ireland image

"  BoxID = frm.BoxID"

The variable BoxID has not been defined. Comment out this line as you are not using BoxID anywhere in your code after this assignment or add a new Dim statement to declare BoxID..
Avatar of stang1
stang1

ASKER

Did i define it correctly?.... same error message

Option Compare Database
Public gbl_hold_box As Integer
Public gbl_go_box As Integer
Public glb_box_id As Integer
Option Explicit
   
Private Sub Frame62_AfterUpdate()
Select Case Me.Frame62
      Case 1
        gbl_hold_box = 1
      Case 2
     gbl_go_box = 2
 End Select
End Sub


Private Sub UpdateBoxStatus_Click()
   Dim db As dao.Database
   Dim rs As dao.Recordset
   Dim frm As Form
   
   Dim hold_value As Integer
   Dim release_Value As Integer
   
   Dim BoxID As Integer
   
Set frm = Forms!PutBoxOnHold
Set db = CurrentDb
  ' BoxID = frm.BoxID
   hold_value = 3
   release_Value = 4
   
Set rs = db.OpenRecordset("dbo_BoxInformation", dbOpenDynaset, dbSeeChanges)

With rs
   If gbl_hold_box = 1 Then
      DoCmd.RunSQL "UPDATE BoxInformation SET BoxInformation.BoxStatusID = 3"
                   WHERE BoxInformation.BoxID = " & Forms![PutBoxOnHold]![BoxID] & ""';"
   ElseIf gbl_go_box = 2 Then
      DoCmd.RunSQL "UPDATE BoxInformation SET BoxInformation.BoxStatusID = 4"
                   WHERE BoxInformation.BoxID = " & Forms![PutBoxOnHold]![BoxID] & ""';"
  End If
End With
rs.Close
Set rs = Nothing
Set db = Nothing
End Sub

Isn't the table called dbo_BoxInformation? The Update statement refers to it as just 'BoxInformation' - change this to dbo_BoxInformation..

On an another note, if this is the complete code, there is no reason to define the rs variable and open the table.. The Update statements are based on variables populated before the recordset open and opening the recordset is irrelevant..
Avatar of stang1

ASKER

In SQL Server it's refered to as dbo.BoxInformation...  I've tried it without the dbo, w/ dbo., w/dbo_  It doesn't seem to see it?
Avatar of stang1

ASKER

Here's my latest effort...  User selects from option group on split form....  It gives a variable not defined message:

Option Compare Database
Public gbl_hold_box As Integer
Public gbl_go_box As Integer
Public glb_box_id As Integer
Option Explicit
   
Private Sub Frame62_AfterUpdate()
Select Case Me.Frame62
      Case 1
        gbl_hold_box = 1
      Case 2
        gbl_go_box = 2
 End Select
End Sub


Private Sub UpdateBoxStatus_Click()
   Dim db As dao.Database
   Dim rs As dao.Recordset
   Dim frm As Form
   Dim BoxID As Integer
   
Set frm = Forms!PutBoxOnHold
Set db = CurrentDb
   'BoxID = frm.BoxID
     
Set rs = db.OpenRecordset("dbo_BoxInformation", dbOpenDynaset, dbSeeChanges)

With rs
   If gbl_hold_box = 1 Then
      DoCmd.RunSQL "UPDATE dbo_BoxInformation SET dbo_BoxInformation.BoxStatusID = 3"
Blows Here -   WHERE dbo_BoxInformation.BoxID = " & Forms![PutBoxOnHold]![BoxID] & ""';"
   ElseIf gbl_go_box = 2 Then
      DoCmd.RunSQL "UPDATE dbo_BoxInformation SET dbo_BoxInformation.BoxStatusID = 4"
                   WHERE dbo_BoxInformation.BoxID = " & Forms![PutBoxOnHold]![BoxID] & ""';"
  End If
End With
rs.Close
Set rs = Nothing
Set db = Nothing
End Sub

ASKER CERTIFIED SOLUTION
Avatar of reb73
reb73
Flag of Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of stang1

ASKER

It still isn't committing to DB, but I wanted to award you the pts for your efforts and we got past the error message.  Do you know why the form switches focus when the option button is selected?  I think it prepares to enter a new record?