• Our booking engine at tickets.railforums.co.uk (powered by TrainSplit) helps support the running of the forum with every ticket purchase! Find out more and ask any questions/give us feedback in this thread!

vbYesNo Assistance needed

Status
Not open for further replies.

bunnahabhain

Established Member
Joined
8 Jun 2005
Messages
2,070
Now most of you wont understand what the hell I'm on about, but I'm having a spot of bother with a little bit of VB coding and was wondering if anybody knew how to rectify it.

What I'm having trouble with, is an If Statement with the 'vbYesNo' bit of a Message Box.

Code:
Private Sub cmdDelete_Click()
If lstDisplayName.ListCount = 0 Then
MsgBox "You must select something to Delete from the Database", vbOKOnly, "Error"
Else
MsgBox "Are you sure you want to Delete what you have selected?", vbYesNo, "Question"
       If Response = "Yes" Then
       lstDisplayName.RemoveItem (lstDisplayName.ListIndex)
       lstDisplayID.RemoveItem (lstDisplayID.ListIndex)
       lstDepartment.RemoveItem (lstDepartment.ListIndex)
       lstSalary.RemoveItem (lstSalary.ListIndex)
       lstDisplayGrade.RemoveItem (lstDisplayGrade.ListIndex)
       Else
       End If
End If
End Sub

That is the code, which is for a Delete Button, with the half completed If Statement at the bottom, does anybody know how to get the thing to stop thinking 'Response' and '"Yes"' should be Variables and that they need to be defined?[/code]
 
Sponsor Post - registered members do not see these adverts; click here to register, or click here to log in
R

RailUK Forums

Tom B

Established Member
Joined
27 Jul 2005
Messages
4,602
Response isn't inbuilt - you can shove the output from the MsgBox into any variable you like, but Response is the usual name for it. So you'd need something like this

Code:
Private Sub cmdDelete_Click()
Dim Response as string

 'Whether it's local or global is up to you

If lstDisplayName.ListCount = 0 Then
MsgBox "You must select something to Delete from the Database", vbOKOnly, "Error"
Else
response=MsgBox("Are you sure you want to Delete what you have selected?", vbYesNo, "Question")
       If Response = "Yes" Then
       lstDisplayName.RemoveItem (lstDisplayName.ListIndex)
       lstDisplayID.RemoveItem (lstDisplayID.ListIndex)
       lstDepartment.RemoveItem (lstDepartment.ListIndex)
       lstSalary.RemoveItem (lstSalary.ListIndex)
       lstDisplayGrade.RemoveItem (lstDisplayGrade.ListIndex)
       Else
       End If
End If
End Sub

That's off the top of my head and I've not checked it though. NB you could do with indenting it a bit to make the If/Endif statements more clearly nested.

HTH
 

bunnahabhain

Established Member
Joined
8 Jun 2005
Messages
2,070
I worked out something similar to that, also they are clear in the coding, but not on the forum.

Code:
'This is the code to delete data that has been entered into the program, it will delete an entire row of text, provided that the entire row has data, if it does not then it unfortunately comes up with an error.
Private Sub cmdDelete_Click()
Dim Response As String
    If lstDisplayName.ListCount = 0 Then
    MsgBox "You must select something to Delete from the Database", vbOKOnly, Error
    Else
       Response = MsgBox("Are you sure you want to Delete the selection?", vbYesNo, Error)
           'This part is where the Data is deleted from the fields, provided the user has entered No in the previous message box.
           If Response = vbYes Then
            lstDisplayName.RemoveItem (lstDisplayName.ListIndex)
            lstDisplayID.RemoveItem (lstDisplayID.ListIndex)
            lstDepartment.RemoveItem (lstDepartment.ListIndex)
            lstSalary.RemoveItem (lstSalary.ListIndex)
            lstDisplayGrade.RemoveItem (lstDisplayGrade.ListIndex)
            Else
            MsgBox "The Data has not been deleted", vbOKOnly
            End If
    End If
End Sub

Oddly enough I managed to get the Maximum, Minimum and Average functions of another part of the program working first time. Yet this took me half the damn day.
 
Status
Not open for further replies.

Top