Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Access and Access VBA > Access VBA
|
Access VBA Discuss using VBA for Access programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 31st, 2004, 11:06 AM
Registered User
 
Join Date: Jun 2004
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default Adding or updating record using VBA

I'm very new to VBA and have a master detail form. The master is: Teacher Absences.
Detail #1: [Substitutes available]
Detail #2: [Call Log]

I'd like to track calls made until I find a Substitute to fill an absence. I added a call_log table with absenceid#, subid#, date_time, and status. Status can be: ACCEPTED, DECLINED, REJECTED, WAITING.

Requirements:
1. When I'm on the Available Subs subform, I'd simply like to click a button [Call] prior to calling someone and have it log it. That is, I've called this Sub.

2. When I've found someone for the call, I'll click [Fill Absence], and it will automatically set the call status to APPROVED for the "current substitute" record, then jump to another form to show the Assignment.

3. I'd really like to have the substitute records in the [Substitute Details] form HIGHLIGHTED if I've called them.

When I wrote the code to do requirement#1, the subform for the call log shows #Name? in the fields after I click the [Call] button. Here's the code:

Private Sub cmdCall_Click()
    Dim dbs As Database
    Dim rst As DAO.Recordset
    Dim numsubstitute As Integer
    Dim numabsence As Integer
    Dim bNoRecords As Boolean


    Set dbs = CurrentDb()

    numabsence = Forms![fill absences]!AbsenceID.Value
    numsubstitute = Forms![fill absences]!Available_Substitutes.Form!SubstituteID

    ' Set rst = dbs.OpenRecordset("Absence_Call_Log", dbOpenDynaset)

    bNoRecords = False
    Set rst = Me("absence_call_log").Form.Recordset

    If rst.EOF Then
     bNoRecords = True
    Else
     rst.MoveFirst
     rst.FindFirst ("AbsenceId=" & numabsence & _
                     " and SubstituteID=" & numsubstitute)
    End If

    If bNoRecords Or rst.NoMatch Then
     rst.AddNew
     rst("substituteid") = numsubstitute
     rst("absenceid") = numabsence
     rst("status") = "WAITING"
     rst.Update
    End If

    rst.Close
    Set rst = Nothing

End Sub


IF I open up a recordset for the entire table, then I can find the record but that seems "inefficient", after all, I have the records right there on the subform. The problem seemed to be caused by closing the recordset and setting it equal to nothing. AM I programmatically removing the underlying recordset at that point?

I rewrote the code as:

Private Sub cmdCall_Click()
    Dim dbs As Database
    Dim rst As DAO.Recordset
    Dim numsubstitute As Integer
    Dim numabsence As Integer
    Dim bNoRecords As Boolean

' MsgBox Me.Absence_Call_Log.Form.RecordSource, vbInformation, "Subform's Record Source"

    Set dbs = CurrentDb()

    numabsence = Forms![fill absences]!AbsenceID.Value
    numsubstitute = Forms![fill absences]!Available_Substitutes.Form!SubstituteID

    ' Set rst = dbs.OpenRecordset("Absence_Call_Log", dbOpenDynaset)

    bNoRecords = False

    Set rst = Absence_Call_Log.Form.Recordset.Clone

    If rst.EOF Then
     bNoRecords = True
    Else
     rst.MoveFirst
     rst.FindFirst ("AbsenceId=" & numabsence & _
                     " and SubstituteID=" & numsubstitute)
    End If

    If bNoRecords Or rst.NoMatch Then
     rst.AddNew
     rst("substituteid") = numsubstitute
     rst("absenceid") = numabsence
     rst("status") = "WAITING"
     rst.Update

     ' Unless this line was put in, I would not see the record I added.
     Absence_Call_Log.Form.Requery

    Else
     Absence_Call_Log.Form.Bookmark = rst.Bookmark
    End If

    rst.Close
    Set rst = Nothing

End Sub


This seems to do what I want. I just want clarification that this is a valid way of approaching it, and if possible, ANOTHER WAY of doing it (to assist with learning).

The reason I ask is that I read on P2P that I could use RecordsetClone and not have to refresh the form, but when I did that method, the recordset was always at EOF and it would always attempt to add new rows, and then violate the PK if you clicked on [Call] for the same person twice.

Also, an answer to Requirement #3 above.

Thanks to any/all suggestions!!





Similar Threads
Thread Thread Starter Forum Replies Last Post
Updating database record shrisangeeta Classic ASP Basics 4 February 28th, 2007 05:25 PM
Updating the record in database lwebzem ASP.NET 1.0 and 1.1 Basics 4 March 8th, 2006 09:40 AM
Updating the record in the database lwebzem ASP.NET 1.0 and 1.1 Basics 3 February 28th, 2006 07:57 AM
updating record stoneman Access 1 July 5th, 2005 09:12 AM
Record not updating Tangerine ASP.NET 1.x and 2.0 Application Design 3 March 24th, 2004 12:00 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.