Christine
Christine

Reputation: 31

Pop up alert vb 6

Can someone help me. I'm having a hard time with this. All I need is to display an alert message whenever the medicines expired.My problem is when I got two or more expired medicines it doesn't alert all.Instead it alerts one medicine.Please help.

here is my code

Private Sub Form_Activate()
    On Error Resume Next

    With Main
        .Text4 = Adodc1.Recordset.Fields("MedicineName")
        .Text1.Text = Adodc1.Recordset.Fields("genericname")
        .Text3.Text = Adodc1.Recordset.Fields("StockQuantity")
        .Combo3 = Adodc1.Recordset.Fields("Expmonth")
        .Combo4 = Adodc1.Recordset.Fields("Expday")
        .Combo5 = Adodc1.Recordset.Fields("Expyear")
    End With

    Dim expirationdate As Date
    expirationdate = CDate(Combo3 & "/" & Combo4 & "/" & Combo5)
    datepicker.Value = Format(Now, "MMM-DD-yyyy")
    If datepicker > expirationdate Then
        MsgBox Text4.Text & " is expired ", vbExclamation, "Warning!"
        If MsgBox("Do you want to dispose " & Text4 & "?", vbQuestion + vbYesNo, "Message") = vbYes Then
            Adodc1.Recordset.Delete
        ElseIf vbNo Then
            Exit Sub
        End If
    End If

End Sub

Private Sub Form_Load()
    Adodc1.CommandType = adCmdUnknown
    Adodc1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\clinic.mdb" & ";Persist Security Info=False"
    Adodc1.RecordSource = "select * from inventory order by Expyear asc"
    Adodc1.Refresh
    Adodc1.Refresh
End sub

Upvotes: 0

Views: 4756

Answers (2)

jac
jac

Reputation: 9726

Bond is correct, you need to iterate over your recordset to display the message for each record that is expired.

Private Sub Form_Activate()
    Dim expirationdate As Date

    On Error Resume Next    '<-- this is going to cause a problem if you never check for errors

    Adodc1.Recordset.MoveFirst    'make sure the control is positioned on the first record
    Do While Adodc1.Recordset.EOF = False    'loop over all of the records
        With Main
            .Text4.Text = Adodc1.Recordset.Fields("MedicineName")
            .Text1.Text = Adodc1.Recordset.Fields("genericname")
            .Text3.Text = Adodc1.Recordset.Fields("StockQuantity")
            .Combo3 = Adodc1.Recordset.Fields("Expmonth")
            .Combo4 = Adodc1.Recordset.Fields("Expday")
            .Combo5 = Adodc1.Recordset.Fields("Expyear")
        End With

        expirationdate = CDate(Combo3 & "/" & Combo4 & "/" & Combo5)
        datepicker.Value = Format(Now, "MMM-DD-yyyy")
        If datepicker > expirationdate Then
            MsgBox Text4.Text & " is expired ", vbExclamation, "Warning!"
            If MsgBox("Do you want to dispose " & Text4 & "?", vbQuestion + vbYesNo, "Message") = vbYes Then
                Adodc1.Recordset.Delete
            End If
        End If
        Adodc1.Recordset.MoveNext
    Loop

End Sub

Upvotes: 0

Bond
Bond

Reputation: 16311

You need to loop through all of the records in your recordset. Currently, you're operating on the FIRST record only.

Do Until Adodc1.Recordset.EOF

    ' Assign values to textboxes, test date, etc.

    ' Fetch the next record...    
    Adodc1.Recordset.MoveNext

Loop

Upvotes: 1

Related Questions