borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder

AndreaVB | Forum | News | Downloads | Register | Help | Member List | Statistics | Search | PM | Profile

Print This Topic
Previous Topic (query help)Next Topic (quick sort columns in a MSdataGrid/datagrid problems) New Topic New Poll Post Reply
AndreaVB Forum : Database : Errors on trying to add data
Poster Message
Nmacp
Level: Guest


icon Errors on trying to add data

Hi everyone. Can someone help me please? I've written a database that's updateable from vb. My problem is that in one area i can't update the table from the form. I get the errors 3705 and 94 which say that the object's already open and i have a null value.I can't figure out why i've got the problem. The errors are in  SubDisplaySoftUsersData this is referencing data from UpdateCompSoft Details and should run when running frmUpdateApp(frmSoftApp.frm)
I've included all this code. Thanx in anticipation
Option Explicit

Public dbSoftware As Connection
Public rsCompSoft As Recordset
Public rsEmployee As Recordset
Public rsCompare As Recordset
Public UsingSoftware As Long 'by definition the autonumber is long thats why you make it  a long'


Sub Main()
  
    frmUpdateApp.Show

End Sub

Sub InitialiseEmployeeData()

    frmUpdateEmployee.txtEmployeeID = ""
    frmUpdateEmployee.txtFirstName = ""
    frmUpdateEmployee.txtLastName = ""
    
End Sub

Sub InitialiseCompSoftData()

    frmUpdateCompSoft.txtSoftwareID = ""
    frmUpdateCompSoft.txtSoftwareName = ""
    
    
End Sub

Sub DisplayEmployeeData()

    frmUpdateEmployee.txtEmployeeID = rsEmployee("EmployeeID")
    frmUpdateEmployee.txtFirstName = rsEmployee("FirstName")
    frmUpdateEmployee.txtLastName = rsEmployee("LastName")
    
End Sub

Sub DisplayCompSoftData()

    frmUpdateCompSoft.txtSoftwareID = rsCompSoft("SoftwareID")
    frmUpdateCompSoft.txtSoftwareName = rsCompSoft("SoftwareName")
  
End Sub


Sub DisplaySoftUsersData()

    frmUpdateSoftwareUser.txtEmployeeID = rsEmployee("EmployeeID")
    frmUpdateSoftwareUser.txtLastName = rsEmployee("LastName")
    frmUpdateSoftwareUser.txtFirstName = rsEmployee("FirstName")
    frmUpdateSoftwareUser.txtSoftwareID = rsCompSoft("SoftwareID")
test:
MsgBox (Str(Err.Number))
    'Complaining:Object already open & of null value as the
    'text entered isn't contained in database
    frmUpdateSoftwareUser.txtSoftwareName = rsCompSoft("SoftwareName")
    
    Call OpenCompareQuery 'Opens a query from the database SQL'

    If rsCompare.RecordCount > 0 Then 'means that an employee record is greater than 0 and an employee has been found'
        frmUpdateSoftwareUser.txtUseSoft = "Yes"
        UsingSoftware = rsCompare("Sequence") 'Saved the sequence'
        frmUpdateSoftwareUser.cmdChangeSoftUsage.Caption = "Using Software"
    Else
        frmUpdateSoftwareUser.txtUseSoft = "No" 'if no add a caption add Employed'
        frmUpdateSoftwareUser.cmdChangeSoftUsage.Caption = "Not Using Software"
    End If
    
    Call CloseCompareQuery
                          
End Sub

Sub WriteEmployeeData()

    rsEmployee("EmployeeID") = frmUpdateEmployee.txtEmployeeID
    rsEmployee("LastName") = frmUpdateEmployee.txtLastName
    rsEmployee("FirstName") = frmUpdateEmployee.txtFirstName
  
    
End Sub

Sub WriteCompSoftData()

    rsCompSoft("SoftwareID") = frmUpdateSoftwareUser.txtSoftwareID
    rsCompSoft("SoftwareName") = frmUpdateSoftwareUser.txtSoftwareName
    
    
    
End Sub
Sub OpenEmployeeTable()

    rsEmployee.Open "Employee", _
        dbSoftware, adOpenStatic, adLockOptimistic, adCmdTableDirect
    
End Sub

Sub OpenCompSoftTable()

    On Error GoTo test
    rsCompSoft.Open "CompSoft", _
        dbSoftware, adOpenStatic, adLockOptimistic, adCmdTableDirect
test:
MsgBox (Str(Err.Number))
'The operation requested by the application is not allowed
'if the object is open.

End Sub

Sub OpenCompareTable()
    
    rsCompare.Open "SoftUsers", _
        dbSoftware, adOpenStatic, adLockOptimistic, adCmdTableDirect
    
End Sub

Sub OpenCompareQuery()

    Dim SQLStr As String
    
    SQLStr = "SELECT * FROM [SoftUsers] " & _
            "WHERE [EmployeeID] = '" & rsEmployee("EmployeeID") & "' " & _
            "AND [SoftwareID] = '" & rsCompSoft("SoftwareID") & "'"
    
    rsCompare.Open SQLStr, _
        dbSoftware, adOpenStatic, adLockOptimistic
    
End Sub

Sub OpenDatabase()
    
    Set dbSoftware = New ADODB.Connection
    dbSoftware.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
        "Data Source=Software.mdb;"
        
    Set rsCompSoft = New Recordset 'set means create objects but do nothing with them'
    Set rsEmployee = New Recordset
    Set rsCompare = New Recordset

    
End Sub

Sub CloseEmployeeTable()

    rsEmployee.Close
      
End Sub

Sub CloseCompSoftTable()

    rsCompSoft.Close
      
End Sub

Sub CloseCompareTable()

    rsCompare.Close
      
End Sub

Sub CloseCompareQuery()

    rsCompare.Close
      
End Sub

Sub CloseDatabase()

    dbSoftware.Close
    Set dbSoftware = Nothing 'removes object from memory'
    Set rsCompSoft = Nothing
    Set rsEmployee = Nothing
    Set rsCompare = Nothing
      
End Sub
---------------------------------------
Option Explicit

Private Sub cmdAdd_Click()

    Call InitialiseCompSoftData
    cmdUpdate.Caption = "Save Details"
        
End Sub

Private Sub cmdExit_Click()

    Unload Me
    
End Sub

Private Sub cmdNext_Click()

    rsCompSoft.MoveNext
    If Not rsCompSoft.EOF Then
        Call DisplayCompSoftData
    Else
        MsgBox ("End of file")
        rsCompSoft.MoveLast
    End If
    
End Sub

Private Sub cmdPrevious_Click()

    rsCompSoft.MovePrevious
    If Not rsCompSoft.BOF Then
        Call DisplayCompSoftData
    Else
        MsgBox ("Beginning of file")
        rsCompSoft.MoveFirst
    End If
    
End Sub

Private Sub cmdUpdate_Click()

    If cmdUpdate.Caption = "Save details" Then
        Call WriteCompSoftData
        rsCompSoft.Update
    Else
        rsCompSoft.AddNew
        Call WriteCompSoftData
        rsCompSoft.Update
        cmdUpdate.Caption = "Update"
    End If
    
End Sub

Private Sub Form_Load()

    Me.Left = 700
    Me.Top = 200
    Call OpenCompSoftTable
    Call DisplayCompSoftData
    frmUpdateApp.mnuFile.Enabled = False
    
End Sub

Private Sub Form_Unload(Cancel As Integer)

    frmUpdateApp.mnuFile.Enabled = True
    Call CloseCompSoftTable
    
End Sub
---------------------------------------
Option Explicit

Private Sub MDIForm_Load()

    Call OpenDatabase
    
End Sub

Private Sub mnuExit_Click()

    Call CloseDatabase
    Unload Me
    
End Sub
Private Sub mnuUpdateSoftwareUsers_Click()

    frmUpdateSoftwareUser.Show

End Sub

Private Sub mnuUpdateEmployeeData_Click()

    frmUpdateEmployee.Show
    
End Sub

Private Sub mnuUpdateCompanySoftware_Click()

    frmUpdateCompSoft.Show
    
End Sub


02-10-2002 at 09:25 PM
| Quote Reply
JLRodgers
Level: Moderator

Registered: 04-04-2002
Posts: 1617
icon Re: Errors on trying to add data

You said that it gives two errors, offhand I did see two things that may fix the problem (wich is good to put anyway)

For the "Already opened"
' The follow will see if the object's open, if so, then it'd close it before opening, or just open if the object isn't already open
If recordsetobect.State = adStateOpen Then ' adStateOpen=1
     recordsetobject.Close
' Then if you need to open the object
     recordsetobject.Open 'connectionstuff
Else
     recordsetobject.Open 'connectionstuff
End if

For the NULL value:
'For any field in the table that can have a null value, put the following in the code:
'(where rs is the recordsetobject)
If IsNull(rs.Fields("fieldname")) Then
     'Stuff to show if the value's null
Else
     'Stuff to do if it's not null
End if

'-OR-
If Not IsNull(rs.Fields("fieldname")) Then
' Stuff to do if it's not null
End If



[Edited by JLRodgers on 03-10-2002 at 01:24 PM GMT]

03-10-2002 at 07:19 PM
View Profile Send Email to User Show All Posts Visit Homepage | Quote Reply
AndreaVB Forum : Database : Errors on trying to add data
Previous Topic (query help)Next Topic (quick sort columns in a MSdataGrid/datagrid problems) New Topic New Poll Post Reply
Surf To:


Not Logged In? Username: Password: Lost your password?
Partners: Download Actual Software | Free Software Download
borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder