Visual Basic Code , VB.NET Code, VB Code
  Home   :  Code   :  Forums   :  Submit   :  Mailing List   :  About   :  Contact


Comparing Input with records in a Database


Comparing Input with records in a Database

Author
Message
Hydrolysis
Hydrolysis
Forum God
Forum God (1.4K reputation)Forum God (1.4K reputation)Forum God (1.4K reputation)Forum God (1.4K reputation)Forum God (1.4K reputation)Forum God (1.4K reputation)Forum God (1.4K reputation)Forum God (1.4K reputation)Forum God (1.4K reputation)

Group: Forum Members
Posts: 5, Visits: 17
Hello all, currently I am developing a Library management Software for my school library as part of my school project. The software is supposed to save records of people that borrow books, those that wish to return, list of books and some other features.

Right now the problem I have is this: I want students to return books using the Check In form. Now a record of the students transaction(when he borrowed the book) has been saved in a database which I have connected to.I want to compare the input with the record in database and if it matches the book is returned. However, whenever I want to return the book I get "Run-time error 91, Object variable or With block variable not set."

I am using an Access file. I would appreciate any help at all given to me. The code below is for the function Deposit books which is called when I click the check in button

Function DepositBooks()

Dim Check As Boolean
    With Form1.Data1
        .Refresh
        Form1.Data1.Recordset.Fields("Name") = Form1.Text1.Text
        Form1.Data1.Recordset.Fields("Year") = Form1.Text2.Text
        Form1.Data1.Recordset.Fields("Book Title") = Form1.Text3.Text
        Form1.Data1.Recordset.Fields("Author") = Form1.Text4.Text
        Form1.Data1.Recordset.Fields("Lib code") = Form1.Text5.Text
        Form1.Data1.Recordset.Fields("Date Borrowed") = Form1.Label10.Caption
        Form1.Data1.Recordset.Fields("Return Date") = Form1.Label9.Caption
                If .Recordset.Fields("Name") = Form1.Text1.Text Then
                    If .Recordset.Fields("Book Title") = Form1.Text3.Text Then
                    Form1.Data1.Recordset.Fields("Lib code") = "ACC 007"
                        MsgBox "Book Deposited.", vbOKOnly, "Deposited"
                        Check = True
                        Exit Function
                    Else
                        MsgBox "This book was not issued to this student."
                        Exit Function
                    End If
                Else
                    .Recordset.MoveNext
                End If
           
           
       
           
       
    End With
   
End Function
Thanks for reading.

TallOne
TallOne
Forum God
Forum God (51K reputation)Forum God (51K reputation)Forum God (51K reputation)Forum God (51K reputation)Forum God (51K reputation)Forum God (51K reputation)Forum God (51K reputation)Forum God (51K reputation)Forum God (51K reputation)

Group: Forum Members
Posts: 370, Visits: 212
What line is the error occuring?  If on the .Recordset.MoveNext, you might need a check something like

If NOT .Recordset.EOF THen
  .Recordset.MoveNext
End If

TallOne

Hydrolysis
Hydrolysis
Forum God
Forum God (1.4K reputation)Forum God (1.4K reputation)Forum God (1.4K reputation)Forum God (1.4K reputation)Forum God (1.4K reputation)Forum God (1.4K reputation)Forum God (1.4K reputation)Forum God (1.4K reputation)Forum God (1.4K reputation)

Group: Forum Members
Posts: 5, Visits: 17
This is the line of code that is shown to be erroneous:

               Form1.Data1.Recordset.Fields("Name") = Form1.Text1.Text

Thanks

TallOne
TallOne
Forum God
Forum God (51K reputation)Forum God (51K reputation)Forum God (51K reputation)Forum God (51K reputation)Forum God (51K reputation)Forum God (51K reputation)Forum God (51K reputation)Forum God (51K reputation)Forum God (51K reputation)

Group: Forum Members
Posts: 370, Visits: 212
Are you sure Name is a field in Data1??

Try...

    With Me.Data1
        .Refresh
        .Recordset.Fields("Name") = Form1.Text1.Text
        .Recordset.Fields("Year") = Form1.Text2.Text
        .Recordset.Fields("Book Title") = Form1.Text3.Text
        .Recordset.Fields("Author") = Form1.Text4.Text
        .Recordset.Fields("Lib code") = Form1.Text5.Text
        .Recordset.Fields("Date Borrowed") = Form1.Label10.Caption
        .Recordset.Fields("Return Date") = Form1.Label9.Caption
                If .Recordset.Fields("Name") = Form1.Text1.Text Then
                    If .Recordset.Fields("Book Title") = Form1.Text3.Text Then
                        .Recordset.Fields("Lib code") = "ACC 007"
                        MsgBox "Book Deposited.", vbOKOnly, "Deposited"
                        Check = True
                        Exit Function
                    Else
                        MsgBox "This book was not issued to this student."
                        Exit Function
                    End If
                Else
                    .Recordset.MoveNext
                End If
    End With

TallOne

Hydrolysis
Hydrolysis
Forum God
Forum God (1.4K reputation)Forum God (1.4K reputation)Forum God (1.4K reputation)Forum God (1.4K reputation)Forum God (1.4K reputation)Forum God (1.4K reputation)Forum God (1.4K reputation)Forum God (1.4K reputation)Forum God (1.4K reputation)

Group: Forum Members
Posts: 5, Visits: 17
Thanks for the reply

Yes I am sure that Name is a valid field in Data1.

When I tried the Me keyword I got: "Invalid use of Me keyword"

Keithuk
Keithuk
Forum God
Forum God (304K reputation)

Group: Moderators
Posts: 1.9K, Visits: 5.5K
Hydrolysis (3/13/2009)

Function DepositBooks()

Dim Check As Boolean
    With Form1.Data1
        .Refresh
        Form1.Data1.Recordset.Fields("Name") = Form1.Text1.Text
        Form1.Data1.Recordset.Fields("Year") = Form1.Text2.Text
        Form1.Data1.Recordset.Fields("Book Title") = Form1.Text3.Text
        Form1.Data1.Recordset.Fields("Author") = Form1.Text4.Text
        Form1.Data1.Recordset.Fields("Lib code") = Form1.Text5.Text
        Form1.Data1.Recordset.Fields("Date Borrowed") = Form1.Label10.Caption
        Form1.Data1.Recordset.Fields("Return Date") = Form1.Label9.Caption
                If .Recordset.Fields("Name") = Form1.Text1.Text Then
                    If .Recordset.Fields("Book Title") = Form1.Text3.Text Then
                    Form1.Data1.Recordset.Fields("Lib code") = "ACC 007"
                        MsgBox "Book Deposited.", vbOKOnly, "Deposited"
                        Check = True
                        Exit Function
                    Else
                        MsgBox "This book was not issued to this student."
                        Exit Function
                    End If
                Else
                    .Recordset.MoveNext
                End If
    End With
   
End Function

Well nothing to do with you problem but your coding. If your using the With statement in your case Form1.Data1 you don't need to specify this again until you End With.

 

Function DepositBooks()

Dim Check As Boolean

With Form1.Data1
        .Refresh
        .Recordset.Fields("Name") = Form1.Text1.Text
        .Recordset.Fields("Year") = Form1.Text2.Text
        .Recordset.Fields("Book Title") = Form1.Text3.Text
        .Recordset.Fields("Author") = Form1.Text4.Text
        .Data1.Recordset.Fields("Lib code") = Form1.Text5.Text
        .Data1.Recordset.Fields("Date Borrowed") = Form1.Label10.Caption
        .Data1.Recordset.Fields("Return Date") = Form1.Label9.Caption
                If .Recordset.Fields("Name") = Form1.Text1.Text Then
                    If .Recordset.Fields("Book Title") = Form1.Text3.Text Then
                        .Recordset.Fields("Lib code") = "ACC 007"
                        MsgBox "Book Deposited.", vbOKOnly, "Deposited"
                        Check = True
                        Exit Function
                    Else
                        MsgBox "This book was not issued to this student."
                        Exit Function
                    End If
                Else
                    .Recordset.MoveNext
                End If
End With
    
End Function


I would also suggest you use the VB prefixes when you name controls it makes the code easier to follow not Form1, Text5, Label10 Wink

Keith

I've been programming with VB for 17 years. Started with VB4 16bit Pro, VB5 Pro, VB6 Pro/Enterprise and now VB3 Pro. But I'm no expert, I'm still learning.

Mark
Mark
Forum God
Forum God (144K reputation)

Group: Moderators
Posts: 1.1K, Visits: 11K
Does it work if you comment out the line that is causing the error?

Does it work if you wrap the field name in square brackets?
.Recordset.Fields("[Name]") = Form1.Text1.Text

Hydrolysis
Hydrolysis
Forum God
Forum God (1.4K reputation)Forum God (1.4K reputation)Forum God (1.4K reputation)Forum God (1.4K reputation)Forum God (1.4K reputation)Forum God (1.4K reputation)Forum God (1.4K reputation)Forum God (1.4K reputation)Forum God (1.4K reputation)

Group: Forum Members
Posts: 5, Visits: 17
Keithuk: Yeah you're right I should make the code easier, I'll do that after I solve the problemBigGrin

Mark: Thanks but I still get: "Object variable or With object variable not set"

Keithuk
Keithuk
Forum God
Forum God (304K reputation)

Group: Moderators
Posts: 1.9K, Visits: 5.5K
Hydrolysis (3/18/2009)
Keithuk: Yeah you're right I should make the code easier, I'll do that after I solve the problemBigGrin

Mark: Thanks but I still get: "Object variable or With object variable not set"

Have you bound your controls to the database RecordSets? Wink

Keith

I've been programming with VB for 17 years. Started with VB4 16bit Pro, VB5 Pro, VB6 Pro/Enterprise and now VB3 Pro. But I'm no expert, I'm still learning.

Hydrolysis
Hydrolysis
Forum God
Forum God (1.4K reputation)Forum God (1.4K reputation)Forum God (1.4K reputation)Forum God (1.4K reputation)Forum God (1.4K reputation)Forum God (1.4K reputation)Forum God (1.4K reputation)Forum God (1.4K reputation)Forum God (1.4K reputation)

Group: Forum Members
Posts: 5, Visits: 17
If you mean setting the datasource and field of the controls to that of the fields in Data1 in Design mode, no I haven't because it always displays the data when I set the DataField. I thought my code covers data entry into the database well, at least the Borrow form in my project that uses similar codes works well without a hitch and the controls aren't bound either.
GO


Similar Topics


Reading This Topic


Login
Existing Account
Email Address:


Password:


Social Logins

Select a Forum....

















A1VBCode Forums


Search