Solved MsgBox

TipsyWolf

Member
Local time
Today, 04:56
Joined
Mar 20, 2024
Messages
223
hey guys,
i was just trying to play with vba, to see if i can code something at least. might take it as a basic vba lesson :)

i wanted to add some OK CANCEL confirmation before delete action. but something went wrong :)

Code:
Dim DelPic As String
DelPic = Me.picafter1filepath

If IsNull(Me.picafter1filepath) Or Me.picafter1filepath = "" Then
MsgBox "No Picture to Delete", vbInformation, "There is not much to delete"

Else        'Execute
MsgBox "Are u sure ?", vbOKCancel, "You are about to delete this picture"
If Response = vbYes Then
MyString = "Yes"

Kill DelPic
Me.picafter1filepath = ""
Me.picafter1picname = ""
Me.Refresh

Else
MyString = "No"
End If
End If

End Sub

could u explain me my mistake please
 
but something went wrong
Would you care to tell us what went wrong? Are you getting a compile error? a runtime error? the wrong results? I can't even tell what procedure the code is running in.

Doesn't look to me like this validation will do anything to stop bad data from being saved.
 
the wrong results?
yes, when i click cancel - it does nothing - its OK
when i click OK - it does nothing - its not ok - it should delete picture (its link and name)
 
Code:
dim rsp as long  ' msgboxes return long
rsp = MsgBox("Are u sure ?", vbOKCancel, "You are about to delete this picture")
if rsp = vbOK then

else
 
What event is the code running in? You can't post code out of context. It is meaningless.
 
Code:
dim rsp as long  ' msgboxes return long
rsp = MsgBox("Are u sure ?", vbOKCancel, "You are about to delete this picture")
if rsp = vbOK then

else

oh, its a bit different than i would think.
it worked !
Code:
Dim DelPic As String
DelPic = Me.picafter1filepath

If IsNull(Me.picafter1filepath) Or Me.picafter1filepath = "" Then
MsgBox "No Picture to Delete", vbInformation, "There is not much to delete"

Else        'Execute
Dim rsp As Long  ' msgboxes return long
rsp = MsgBox("Are u sure ?", vbOKCancel, "You are about to delete this picture")
If rsp = vbOK Then

Kill DelPic
Me.picafter1filepath = ""
Me.picafter1picname = ""
Me.Refresh

End If
End If
End Sub

thank u very much. will continue explore vba with your help.
thank you guys !
 
hey guys,
i was just trying to play with vba, to see if i can code something at least. might take it as a basic vba lesson :)

i wanted to add some OK CANCEL confirmation before delete action. but something went wrong :)

could u explain me my mistake please

Test with this code

Code:
Dim DelPic As String
Dim Response as long

DelPic = Me.picafter1filepath

If IsNull(Me.picafter1filepath) Or Me.picafter1filepath = "" Then
      MsgBox "No Picture to Delete", vbInformation, "There is not much to delete"
Else        
     'Execute
     Response =MsgBox ("Are u sure ?", vbOKCancel, "You are about to delete this picture")
     If Response = vbOk Then
           MyString = "Yes"
           Kill DelPic
           Me.picafter1filepath = ""
           Me.picafter1picname = ""
           Me.Refresh
     Else
           MyString = "No"
     End If
End If

End Sub
 
Test with this code

Code:
Dim DelPic As String
Dim Response as long

DelPic = Me.picafter1filepath

If IsNull(Me.picafter1filepath) Or Me.picafter1filepath = "" Then
      MsgBox "No Picture to Delete", vbInformation, "There is not much to delete"
Else       
     'Execute
     Response =MsgBox ("Are u sure ?", vbOKCancel, "You are about to delete this picture")
     If Response = vbOk Then
           MyString = "Yes"
           Kill DelPic
           Me.picafter1filepath = ""
           Me.picafter1picname = ""
           Me.Refresh
     Else
           MyString = "No"
     End If
End If

End Sub
it worked as well ! :) thank you!
 
FYI. Often we call message box function without using the return value. In that case you cannot use Parentheses (unless including Call keyword). But to use the return the value you need the ()

No return:
Code:
msgbox "Some Message", vbinformation

Return
Code:
rsp = msgbox("Do you want to continue?',VbYesNo)
if rsp = vbNo then
else
end if

Also make sure all modules have option explicit to require variable declaration. I say that because you use "Response". That looks like a variable but never declared. This can cause a lot of problems. You demoed one.
If Response = vbYes Then

also do not do this. This causes problems
Code:
Me.picafter1filepath = ""
Me.picafter1picname = ""
Instead
Code:
Me.picafter1filepath = NULL
Me.picafter1picname = NULL
 
Also if you possibly have nulls and empty strings in controls the easier check
Code:
If IsNull(Me.picafter1filepath) Or Me.picafter1filepath = ""
is
Code:
If Me.picafter1filepath & "" = ""
 

Users who are viewing this thread

Back
Top Bottom