Hello Dear All,
I have a small problem ,though seems very simple,being a newb It is difficult for me. Please Help me
I have a message box VbYesNoCancel
My code is here

If ACRS.RecordCount>0 Then
MsgBox"SALARY ALREADY CALCULATED DO YOU WANT TO RECALCULATE ?",vbYesNoCancel,"Pebble"
Else
CmbNdm.SetFocus
End If

My problem is If click "Yes" I should proceed for Recalculation, If I click "No" I should exit Sub

Dani AI

Generated

Good catch from and confirmation from — checking the MsgBox return value is the right approach. A couple of short improvements make the flow more robust: handle vbCancel explicitly (it’s different from No), and avoid relying on RecordCount unless the cursor type guarantees it.

' Classic VB / ADO - more robust record check and explicit Cancel handling
Dim resp As VbMsgBoxResult

If Not (ACRS.BOF And ACRS.EOF) Then
    resp = MsgBox("Salary already calculated. Recalculate? This will overwrite prior results.", vbYesNoCancel + vbQuestion, "Pebble")
    Select Case resp
        Case vbYes
            ' Call a Recalculate routine here
        Case vbNo
            Exit Sub
        Case vbCancel
            ' Cancel chosen: restore focus or abort gracefully
            CmbNdm.SetFocus
            Exit Sub
    End Select
Else
    CmbNdm.SetFocus
End If

For readers on VB.NET, the same logic uses MessageBox and DialogResult:

' VB.NET example
Dim result As DialogResult = MessageBox.Show("Salary already calculated. Recalculate?", "Pebble", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question)
If result = DialogResult.Yes Then
    ' Recalculate
ElseIf result = DialogResult.No Then
    Return
Else
    ' Cancel handling (restore focus, etc.)
    CmbNdm.Focus()
End If

Notes and troubleshooting tips: RecordCount can return -1 with forward-only cursors; prefer If Not (rs.BOF And rs.EOF) Then or use an appropriate cursor type (adOpenStatic) when RecordCount is required. Make the prompt explicit about consequences (overwriting data). For long recalculations, run the work off the UI thread or show progress and disable relevant controls so the user cannot trigger duplicate operations.

Recommended Answers

All 3 Replies

Hi mavtcr, try the following and let us know how it goes:

If ACRS.RecordCount > 0 Then
        If MsgBox("SALARY ALREADY CALCULATED DO YOU WANT TO RECALCULATE ?", vbYesNoCancel, "Pebble") = vbYes Then
            'Perform your Yes calculation here
        Else
            'Prompt a message for no or do nothing
        End If
    Else
        CmbNdm.SetFocus
    End If

Thank u Stuugie.. It works

No problem mavtcr, if your issue has been resolved please don't forget to mark this thread as solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.