Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VB to SQL Server Connection

Status
Not open for further replies.

Ausburgh

Programmer
Jul 21, 2004
62
0
0
US
Sorry for the long code (below) but I am still trying to get a connection to the SQL server. When I run the app, I get a run-time error '3704' with the description Operation is not allowed when the object is closed

Please help! I've attached my entire code for the data entry form:

Code:
Option Explicit
    Dim ABranch As String
    Dim ASal As Double
    Dim AAwd As Double
    Dim ABen As Double
    Dim AOT As Double
    Dim ATrav As Double
    Dim ATrans As Double
    Dim ATraining As Double
    Dim ARCU As Double
    Dim APrn As Double
    Dim AContract As Double
    Dim ASupplies As Double
    Dim ASuppliesType As String
    Dim APO As Double
    Dim ACC As Double
    Dim AGenOff As Double
    Dim ACOSA As Double
    Dim AOfficeDepot As Double
    Dim AEquip As Double
    Dim ALand As Double
    Dim ATort As Double
    Dim AInterest As Double
    Dim ARemark As String
    Dim TotalBranchAllocation As Double
    
Dim Conn As ADODB.Connection
Dim rs As ADODB.Recordset

Private Sub Form_Unload(Cancel As Integer)
    Set Conn = Nothing
    Set rs = Nothing
    Unload Me
End Sub
   
Private Sub cboABranch_LostFocus()
     'If ABranch = "" Then
        'MsgBox ("You must pick a branch")
        'cboABranch.SetFocus
     'End If
End Sub

Private Sub cmdADel_Click()
    Dim check
    Dim Cancel
    Beep
    check = MsgBox(" Are you sure you want to delete Current Record? ", vbQuestion + vbYesNo, "Confirm")

    If check = vbYes Then
        rs.Delete
        rs.MoveNext
    Else
        Cancel = True
    End If
End Sub

Private Sub cmdANew_Click()
    rs.AddNew
End Sub

Private Sub cmdAReset_Click()
    Dim check
    Dim Cancel
    Beep
    check = MsgBox(" Are you sure you want to ERASE every field? ", vbQuestion + vbYesNo, "Confirm")

    If check = vbYes Then
         rs.update
        
    Else
        Cancel = True
    End If
End Sub

Private Sub cmdNav_Click(Index As Integer)
    
    'If enteringnew Then Exit Sub
    
    Select Case Index
        Case 0      'First Record
            'RecNav.MoveFirst
            rs.MoveFirst
        Case 1      'Previous Record
            'RecNav.MoveBack
            rs.MovePrevious
        Case 2      'Next Record
            'RecNav.MoveForward
            rs.MoveNext
        Case 3      'Last Record
            'RecNav.MoveLast
            rs.MoveLast
    End Select
        
End Sub

Private Sub Form_Load()
    'Me.WindowState = vbMaximized
    '**********************************************
    
    'Set and make the connection to the database.
    Set Conn = New ADODB.Connection
    
    '**********
    'define the recordset access statement
    Dim strSQL As String
    strSQL = "SELECT * Allocation"
    Set rs = New ADODB.Recordset
    '************
    
    On Error GoTo Err_Exit
    Conn.ConnectionString = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=dbFSF;Data Source=URO -ABC0001"
    Conn.Open
    
    With rs
        .CursorLocation = adUseClient
        .CursorType = adOpenDynamic
        .LockType = adLockOptimistic
    End With
    
Exit Sub

Err_Exit:
    ADOerror
    '**************************        
    cboABranch.Text = ""
    cboASuppliesType.Text = ""
    txtASal.Text = 0
    txtAAwd.Text = 0
    txtABen.Text = 0
    txtAOT.Text = 0
    txtATrav.Text = 0
    txtATrans.Text = 0
    txtATraining.Text = 0
    txtARCU.Text = 0
    txtAPrn.Text = 0
    txtAContract.Text = 0
    lblASupResult.Caption = "0.00"
    txtACC.Text = 0
    txtAPO.Text = 0
    'txtAGenOff.Text = 0
    txtACOSA.Text = 0
    txtAOfficeDepot.Text = 0
    txtAEquip.Text = 0
    txtALand.Text = 0
    txtATort.Text = 0
    txtAInterest.Text = 0
    txtARemark.Text = ""
    txtTotalAlloc = 0
    
    cboABranch.Text = rs!cboABranch
    cboASuppliesType.Text = rs!cboASuppliesType
    txtASal.Text = rs!txtASal
    txtAAwd.Text = rs!txtAAwd
    txtABen.Text = rs!txtABen
    txtAOT.Text = rs!txtAOT
    txtATrav.Text = rs!txtATrav
    txtATrans.Text = rs!txtATrans
    txtATraining.Text = rs!txtATraining
    txtARCU.Text = rs!txtARCU
    txtAPrn.Text = rs!txtAPrn
    txtAContract.Text = rs!txtAContract
    lblASupResult.Caption = rs!lblASupResult
    txtACC.Text = rs!txtACC
    txtAPO.Text = rs!txtAPO
    txtAGenOff.Text = rs!txtAGenOff
    txtACOSA.Text = rs!txtACOSA
    txtAOfficeDepot.Text = rs!txtAOfficeDepot
    txtAEquip.Text = rs!txtAEquip
    txtALand.Text = rs!txtALand
    txtATort.Text = rs!txtATort
    txtAInterest.Text = rs!txtAInterest
    txtARemark.Text = rs!txtARemark
    txtTotalAlloc = rs!txtTotalAlloc
    
End Sub

Private Sub cmdAClose_Click()
    Unload Me
    Load Splash
    Splash.Show vbModeless
End Sub

Private Sub cmdASave_Click()

    ABranch = cboABranch.Text
    ASuppliesType = cboASuppliesType.Text
    ASal = CDbl(txtASal.Text)
    AAwd = CDbl(txtAAwd.Text)
    ABen = CDbl(txtABen.Text)
    AOT = CDbl(txtAOT.Text)
    ATrav = CDbl(txtATrav.Text)
    ATrans = CDbl(txtATrans.Text)
    ATraining = CDbl(txtATraining.Text)
    ARCU = CDbl(txtARCU.Text)
    APrn = CDbl(txtAPrn.Text)
    AContract = CDbl(txtAContract.Text)
    APO = CDbl(txtAPO.Text)
    ACC = CDbl(txtACC.Text)
    ACOSA = CDbl(txtACOSA.Text)
    AOfficeDepot = CDbl(txtAOfficeDepot.Text)
    'AGenOff = CDbl(txtAGenOff.Text)
    AGenOff = CDbl(ACOSA + AOfficeDepot)
    txtAGenOff = FormatCurrency(AGenOff)
    ASupplies = CDbl(APO + ACC + AGenOff)
    lblASupResult.Caption = FormatCurrency(ASupplies)
    AEquip = CDbl(txtAEquip.Text)
    ALand = CDbl(txtALand.Text)
    ATort = CDbl(txtATort.Text)
    AInterest = CDbl(txtAInterest.Text)
    ARemark = txtARemark.Text
    
    'ASal and ABen not included
    TotalBranchAllocation = CDbl(AAwd + AOT + ATrav + ATrans + ATraining + _
                                ARCU + APrn + AContract + ASupplies + _
                                AEquip + ALand + ATort + AInterest)
    txtTotalAlloc = FormatCurrency(TotalBranchAllocation)
End Sub

Private Sub ADOerror()
'/ Enumerate the Errors collection and display properties of each Error object
   Dim errCollection As Variant
   Dim errLoop  As Error
   Dim strError As String
   Dim iCounter As Integer

   On Error Resume Next    '/ in case ADO connection not set or other init problems
   iCounter = 1
   strError = " "
   For Each errLoop In errCollection
      With errLoop
         strError = _
            "Error #" & iCounter & vbCrLf & _
            "  ADO Error #" & .Number & vbCrLf & _
            "  Description - " & .Description & vbCrLf & _
            "  Error Source - " & .Source & vbCrLf
            Debug.Print strError
            iCounter = iCounter + 1
      End With
   Next
End Sub
 
Try this...

Code:
Private Sub Form_Load()
    'Me.WindowState = vbMaximized
    '**********************************************
    
    'Set and make the connection to the database.
    Set Conn = New ADODB.Connection
    
    '**********
    'define the recordset access statement
    Dim strSQL As String
    strSQL = "SELECT * Allocation"
    Set rs = New ADODB.Recordset
    '************
    
    On Error GoTo Err_Exit
    Conn.ConnectionString = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=dbFSF;Data Source=URO -ABC0001"
    Conn.Open
    
    [!]Call rs.Open(strSQL, Conn)[/!]

    With rs
        .CursorLocation = adUseClient
        .CursorType = adOpenDynamic
        .LockType = adLockOptimistic
    End With
    
Exit Sub

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Sorry, you should put that line just before the Exit Sub.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Some thoughts:
Code:
Private Sub Form_Unload(Cancel As Integer)

    If Not rs Is Nothing Then
       If rs.State = adStateOpen Then rs.Close
       Set rs = Nothing
    End If
    If Not Conn Is Nothing Then 
       If Conn.State = adStateOpen Then Conn.Close
       Set Conn = Nothing
    End If
    Unload Me
End Sub

Code:
Private Sub cmdNav_Click(Index As Integer)
    
    Select Case Index
        Case 0      'First Record
            rs.MoveFirst
        Case 1      'Previous Record
            rs.MovePrevious
            If rs.BOF Then rs.MoveFirst
        Case 2      'Next Record
            rs.MoveNext
            If rs.EOF Then rs.MoveLast
        Case 3      'Last Record
            rs.MoveLast
    End Select
        
End Sub

Code:
With rs
  .ActiveConnection = Conn
  .CursorLocation = adUseServer 'should be faster than adUseClient 
  .CursorType = adOpenDynamic
  .LockType = adLockOptimistic
  .Source = strSQL '"SELECT * FROM Allocation;" Missing FROM  at your line
  .Open
End With
 
Thanks gmmastros & JerryKlmns for responding.

Now I *have a connection but I cannot see any of the current records in the database (hence I cannot edit or update the records).

*However everytime I try to add a new record on the VB side - I see a new record on the SQL side but it's an empty record.

Code:
Option Explicit
    Dim ABranch As String
    Dim ASal As Double
    Dim AAwd As Double
    Dim ABen As Double
    Dim AOT As Double
    Dim ATrav As Double
    Dim ATrans As Double
    Dim ATraining As Double
    Dim ARCU As Double
    Dim APrn As Double
    Dim AContract As Double
    Dim ASupplies As Double
    Dim ASuppliesType As String
    Dim APO As Double
    Dim ACC As Double
    Dim AGenOff As Double
    Dim ACOSA As Double
    Dim AOfficeDepot As Double
    Dim AEquip As Double
    Dim ALand As Double
    Dim ATort As Double
    Dim AInterest As Double
    Dim ARemark As String
    Dim TotalBranchAllocation As Double
    
Dim Conn As ADODB.Connection
Dim rs As ADODB.Recordset

Private Sub Form_Unload(Cancel As Integer)

    If Not rs Is Nothing Then
       If rs.State = adStateOpen Then rs.Close
       Set rs = Nothing
    End If
    If Not Conn Is Nothing Then
       If Conn.State = adStateOpen Then Conn.Close
       Set Conn = Nothing
    End If
    Unload Me
End Sub

   
Private Sub cboABranch_LostFocus()
     'If ABranch = "" Then
        'MsgBox ("You must pick a branch")
        'cboABranch.SetFocus
     'End If
End Sub

Private Sub cmdADel_Click()
    Dim check
    Dim Cancel
    Beep
    check = MsgBox(" Are you sure you want to delete Current Record? ", vbQuestion + vbYesNo, "Confirm")

    If check = vbYes Then
        rs.Delete
        rs.MoveNext
    Else
        Cancel = True
    End If
End Sub

Private Sub cmdANew_Click()
    rs.AddNew
End Sub

Private Sub cmdAReset_Click()
    Dim check
    Dim Cancel
    Beep
    check = MsgBox(" Are you sure you want to ERASE every field? ", vbQuestion + vbYesNo, "Confirm")

    If check = vbYes Then
        rs.Update
    Else
        Cancel = True
    End If
End Sub

Private Sub cmdNav_Click(Index As Integer)
    
    Select Case Index
        Case 0      'First Record
            rs.MoveFirst
        Case 1      'Previous Record
            rs.MovePrevious
            If rs.BOF Then rs.MoveFirst
        Case 2      'Next Record
            rs.MoveNext
            If rs.EOF Then rs.MoveLast
        Case 3      'Last Record
            rs.MoveLast
    End Select
        
End Sub


Private Sub Form_Load()
    
    cboABranch.AddItem ""
    cboABranch.AddItem "EXEC"
    cboABranch.AddItem "AMB"
    cboABranch.AddItem "CLAIMS"
    cboABranch.AddItem "ISB"
    cboABranch.AddItem "POB"
        
    cboASuppliesType.AddItem ""
    cboASuppliesType.AddItem "COSA"
    cboASuppliesType.AddItem "Office Deport"
    cboASuppliesType.AddItem "Credit Card"
    cboASuppliesType.AddItem "Contracts"
    
    cboABranch.Text = ""
    cboASuppliesType.Text = ""
    txtASal.Text = 0
    txtAAwd.Text = 0
    txtABen.Text = 0
    txtAOT.Text = 0
    txtATrav.Text = 0
    txtATrans.Text = 0
    txtATraining.Text = 0
    txtARCU.Text = 0
    txtAPrn.Text = 0
    txtAContract.Text = 0
    lblASupResult.Caption = "0.00"
    txtACC.Text = 0
    txtAPO.Text = 0
    'txtAGenOff.Text = 0
    txtACOSA.Text = 0
    txtAOfficeDepot.Text = 0
    txtAEquip.Text = 0
    txtALand.Text = 0
    txtATort.Text = 0
    txtAInterest.Text = 0
    txtARemark.Text = ""
    txtTotalAlloc = 0
    
    'Set and make the connection to the database.
    Set Conn = New ADODB.Connection
    
    '**********
    'define the recordset access statement
    Dim strSQL As String
    strSQL = "SELECT * FROM Allocation"
    Set rs = New ADODB.Recordset
    '************
    
    On Error GoTo Err_Exit
    Conn.ConnectionString = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=dbFSF;Data Source=URO -ABC0001"
    Conn.Open
    
    With rs
        .ActiveConnection = Conn
        .CursorLocation = adUseServer
        .CursorType = adOpenDynamic
        .LockType = adLockOptimistic
        .Source = strSQL
        .Open
        
    'save to SQL
    ABranch = rs!ABranch
    ASuppliesType = rs!ASuppliesType
    ASal = rs!ASal
    AAwd = rs!AAwd
    ABen = rs!ABen
    AOT = rs!AOT
    ATrav = rs!ATrav
    ATrans = rs!ATrans
    ATraining = rs!ATraining
    ARCU = rs!ARCU
    APrn = rs!APrn
    AContract = rs!AContract
    lblASupResult.Caption = rs!ASupplies
    ACC = rs!ACC
    APO = rs!APO
    AGenOff = rs!AGenOff
    ACOSA = rs!ACOSA
    AOfficeDepot = rs!AOfficeDepot
    AEquip = rs!AEquip
    ALand = rs!ALand
    ATort = rs!ATort
    AInterest = rs!AInterest
    ARemark = rs!ARemark
    TotalBranchAllocation = rs!BranchAllotment
        
    End With
    
'Call rs.Open(strSQL, Conn)
    
Exit Sub

Err_Exit:
    ADOerror
       
End Sub

Private Sub cmdAClose_Click()
    Unload Me
    Load Splash
    Splash.Show vbModeless
End Sub

Private Sub cmdASave_Click()

    ABranch = cboABranch.Text
    ASuppliesType = cboASuppliesType.Text
    ASal = CDbl(txtASal.Text)
    AAwd = CDbl(txtAAwd.Text)
    ABen = CDbl(txtABen.Text)
    AOT = CDbl(txtAOT.Text)
    ATrav = CDbl(txtATrav.Text)
    ATrans = CDbl(txtATrans.Text)
    ATraining = CDbl(txtATraining.Text)
    ARCU = CDbl(txtARCU.Text)
    APrn = CDbl(txtAPrn.Text)
    AContract = CDbl(txtAContract.Text)
    APO = CDbl(txtAPO.Text)
    ACC = CDbl(txtACC.Text)
    ACOSA = CDbl(txtACOSA.Text)
    AOfficeDepot = CDbl(txtAOfficeDepot.Text)
    'AGenOff = CDbl(txtAGenOff.Text)
    AGenOff = CDbl(ACOSA + AOfficeDepot)
    txtAGenOff = FormatCurrency(AGenOff)
    ASupplies = CDbl(APO + ACC + AGenOff)
    lblASupResult.Caption = FormatCurrency(ASupplies)
    AEquip = CDbl(txtAEquip.Text)
    ALand = CDbl(txtALand.Text)
    ATort = CDbl(txtATort.Text)
    AInterest = CDbl(txtAInterest.Text)
    ARemark = txtARemark.Text
    
    'ASal and ABen not included
    TotalBranchAllocation = CDbl(AAwd + AOT + ATrav + ATrans + ATraining + _
                                ARCU + APrn + AContract + ASupplies + _
                                AEquip + ALand + ATort + AInterest)
    txtTotalAlloc = FormatCurrency(TotalBranchAllocation)
    
    'save to SQL
    ABranch = rs!ABranch
    ASuppliesType = rs!ASuppliesType
    ASal = rs!ASal
    AAwd = rs!AAwd
    ABen = rs!ABen
    AOT = rs!AOT
    ATrav = rs!ATrav
    ATrans = rs!ATrans
    ATraining = rs!ATraining
    ARCU = rs!ARCU
    APrn = rs!APrn
    AContract = rs!AContract
    ASupplies = rs!ASupplies
    ACC = rs!ACC
    APO = rs!APO
    AGenOff = rs!AGenOff
    ACOSA = rs!ACOSA
    AOfficeDepot = rs!AOfficeDepot
    AEquip = rs!AEquip
    ALand = rs!ALand
    ATort = rs!ATort
    AInterest = rs!AInterest
    ARemark = rs!ARemark
    TotalBranchAllocation = rs!BranchAllotment
    rs.Update
    
End Sub

Private Sub ADOerror()
'/ Enumerate the Errors collection and display properties of each Error object
   Dim errCollection As Variant
   Dim errLoop  As Error
   Dim strError As String
   Dim iCounter As Integer

   On Error Resume Next    '/ in case ADO connection not set or other init problems
   iCounter = 1
   strError = " "
   For Each errLoop In errCollection
      With errLoop
         strError = _
            "Error #" & iCounter & vbCrLf & _
            "  ADO Error #" & .Number & vbCrLf & _
            "  Description - " & .Description & vbCrLf & _
            "  Error Source - " & .Source & vbCrLf
            Debug.Print strError
            iCounter = iCounter + 1
      End With
   Next
End Sub

 
If you want to update the data in the database, you need to call the recordset object's RS.Update method. You'll probably want to add this to the cmdNav_Click event.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
I think you could use a new command button "Add New Record", which will clear all fields. A second button to actually save the new record. That second button should
A] Take care of validation for data input.
B] Add the new record
C] Assign falues to the fields
D] Save the new record to the database using the Update method
Code:
     With rs
        .AddNew  ' 2nd Step
        .Fields("ABranch") = cboABranch.Text '3rd Step
        .Fields("ASuppliesType") = cboASuppliesType.Text
        .Fields("ASal") = CDbl(txtASal.Text)
        ....
        .Update  ' 4th Step
      End With
 


Thanks JerryKlmns that worked.

I can update (add new records) the database on the SQL Server side now.

Now I need to figure out how to edit, navigate and generally maintain the records ... since I can't see them when I load the application.

 
Now I can see the first record only and I can edit that record only.

Anybody know how I can move to the subsequent records?

I added the following code to my Form_Load():

Code:
With rs
        cboABranch.Text = !ABranch
        cboASuppliesType.Text = !ASuppliesType & ""
        txtASal.Text = !ASal
        txtAAwd.Text = !AAwd & ""
        txtABen.Text = !ABen & ""
        txtAOT.Text = !AOT & ""
        txtATrav.Text = !ATrav & ""
        txtATrans.Text = !ATrans & ""
        txtATraining.Text = !ATraining & ""
        txtARCU.Text = !ARCU & ""
        txtAContract.Text = !AContract & ""
        txtAPrn.Text = !APrn & ""
        lblASupResult.Caption = !ASupplies & ""
        txtAPO.Text = !APO & ""
        txtACC.Text = !ACC & ""
        txtAGenOff.Text = !AGenOff & ""
        txtACOSA.Text = !ACOSA & ""
        txtAOfficeDepot.Text = !AOfficeDepot & ""
        txtAEquip.Text = !AEquip & ""
        txtALand.Text = !ALand & ""
        txtATort.Text = !ATort & ""
        txtAInterest.Text = !AInterest & ""
        txtARemark.Text = !ARemark & ""
        txtTotalAlloc.Text = !BranchAllotment & ""        
    End With
 
Put that same code in the cmdNav_Click event.

Basically, after moving to another record, you need to re-load the values of the text boxes.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 

Thanks George it worked.

However, I find that if I stop on say record #25 and I edit it and save it and then move to another record before I close the app ... record #25 is saved twice deleting the immediate record in the direction in which I moved after I saved record #25 (I hope I explained it clearly).

Any idea why that's happening.

Thanks again
 
Post the code for the cmd_nav click event.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Code:
Private Sub cmdNav_Click(Index As Integer)
    With rs
    
        cboABranch.Text = !ABranch
        cboASuppliesType.Text = !ASuppliesType & ""
        txtASal.Text = !ASal
        txtAAwd.Text = !AAwd & ""
        txtABen.Text = !ABen & ""
        txtAOT.Text = !AOT & ""
        txtATrav.Text = !ATrav & ""
        txtATrans.Text = !ATrans & ""
        txtATraining.Text = !ATraining & ""
        txtARCU.Text = !ARCU & ""
        txtAContract.Text = !AContract & ""
        txtAPrn.Text = !APrn & ""
        lblASupResult.Caption = !ASupplies & ""
        txtAPO.Text = !APO & ""
        txtACC.Text = !ACC & ""
        txtAGenOff.Text = !AGenOff & ""
        txtACOSA.Text = !ACOSA & ""
        txtAOfficeDepot.Text = !AOfficeDepot & ""
        txtAEquip.Text = !AEquip & ""
        txtALand.Text = !ALand & ""
        txtATort.Text = !ATort & ""
        txtAInterest.Text = !AInterest & ""
        txtARemark.Text = !ARemark & ""
        txtTotalAlloc.Text = !BranchAllotment & ""
        
        Select Case Index
            Case 0      'First Record
                .MoveFirst
            Case 1      'Previous Record
                .MovePrevious
                If .BOF Then
                    .MoveFirst
                End If
            Case 2      'Next Record
                .MoveNext
                If .EOF Then
                    .MoveLast
                End If
            Case 3      'Last Record
                .MoveLast
        End Select
    End With
End Sub

Thanks
 
In a previous post:

Basically, [!]after[/!] moving to another record, you need to re-load the values of the text boxes.

So, move that new block of code to the bottom of the procedure (just before the end with)

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
You're right. My eyes are SO tired but everything seems to be fine now.

Thanks George and JerryKlmns; You guys are my heroes.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top