VB School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a VB Expert!

Join 300,396 VB Programmers for FREE! Get instant access to thousands of VB experts, tutorials, code snippets, and more! There are 1,610 people online right now. Registration is fast and FREE... Join Now!




vbOk & vbCancel button choices

 

vbOk & vbCancel button choices

PNJLj

23 Jun, 2009 - 12:00 AM
Post #1

New D.I.C Head
*

Joined: 17 May, 2009
Posts: 48



Thanked: 1 times
My Contributions
CODE
Private Sub cmdSave_Click()
    Dim Answer As VbMsgBoxResult
    
    If lstYours.ListCount = -0 Then
        MsgBox "Cannot save or send as you have nothing in your list.", vbOKOnly + vbExclamation, "Sorry!"
    Else
        Answer = "InputBox2"
        YourFile = InputBox2("Please enter your name:" & vbCrLf & "Thanks!" & vbCrLf & "This will help me determine who's list I am looking at.", "FileName", "EnterNameHere")
        If InputBox2 = vbOK Then
        
        Dim NextItem As Long
        Open "E:\" & YourFile & ".txt" For Append As #1
            ' Loop through the items in the listbox
            For NextItem = 0 To lstYours.ListCount - 1
                ' Write to the file the item text
                Print #1, lstYours.List(NextItem)
            Next
        Close #1
    
        ' Log into the E-Mail server
        EMailSession.SignOn
        EMailMessage.SessionID = EMailSession.SessionID
        ' Create the Message
        EMailMessage.Compose
        EMailMessage.RecipAddress = "blah@hotmail.com"
        EMailMessage.RecipDisplayName = "freddy"
        EMailMessage.MsgSubject = "Here is " & YourFile & "'s list they sent."
        EMailMessage.MsgNoteText = "Hello Freddy, here is my list." & vbCrLf & "Talk Soon!!"
        EMailMessage.AttachmentPathName = "E:\" & YourFile & ".txt"
        ' Send the Message
        EMailMessage.Send
        ' Log Off the E-Mail Server
        EMailSession.SignOff
    
        MsgBox "Your list has been sent to the Programmer (freddy)." & vbCrLf & "Thankyou!", vbOKOnly + vbExclamation, "E-Mail Sent!"
    Else
        MsgBox "You Canceled", vbOKOnly, "Canceled"
        End If
        
    End If
    

End Sub


Okay, from the code above what i am trying to do is tell the program that if the user clicks cancel in the inputbox it tells them that they canceled the operation and will not send the e-mail.
Problem is ... cannot work it out. any help is always appreciated.


Thanks.

P.S - i need the YourFile in front of the inputbox as that is a global string used throughout the program.
I am using VB6

This post has been edited by PNJLj: 23 Jun, 2009 - 12:02 AM

User is offlineProfile CardPM
+Quote Post


NoBrain

RE: VbOk & VbCancel Button Choices

23 Jun, 2009 - 12:38 AM
Post #2

D.I.C Lover
Group Icon

Joined: 25 Mar, 2009
Posts: 1,152



Thanked: 33 times
Dream Kudos: 125
My Contributions
i dont see any problems what errors you get
User is offlineProfile CardPM
+Quote Post

PNJLj

RE: VbOk & VbCancel Button Choices

23 Jun, 2009 - 02:32 AM
Post #3

New D.I.C Head
*

Joined: 17 May, 2009
Posts: 48



Thanked: 1 times
My Contributions
QUOTE(NoBrain @ 23 Jun, 2009 - 12:38 AM) *

i dont see any problems what errors you get


Compile error - Sub or Function not defined

when it gets to the first Inputbox2 code.

just editeded it a little ...
CODE
YourFile = InputBox2 = InputBox("Please enter your name:" & vbCrLf & "Thanks!" & vbCrLf & "This will help me determine who's list I am looking at.", "FileName", "EnterNameHere")


Now all it does is always cancels. i cannot do okay, when i do i get my msgbox from the else section.

This post has been edited by PNJLj: 23 Jun, 2009 - 02:38 AM
User is offlineProfile CardPM
+Quote Post

Cookiesliyr

RE: VbOk & VbCancel Button Choices

23 Jun, 2009 - 03:24 AM
Post #4

D.I.C Head
**

Joined: 16 May, 2009
Posts: 102



Thanked: 6 times
My Contributions
u can do this to know what the user clicked

CODE

MsgButton = Msgbox ("Blah blah", vbokcancel + vbExclamation, "whatever")
if msgbutton = VBok then
' whatever u wanna do
else if msgbutton = VBCancel then
' u can use else here too
end if


will as u can see the msgButton would be variant type so there might be another way to do it like in C++ u can do it as int which is much lighter but as long as u won't declare allot of variant types it is ok

Note: the code might have stupid mistake cause it is not tested XD my pc is too far from here but i am sure u can handle these mistakes

This post has been edited by Cookiesliyr: 23 Jun, 2009 - 03:24 AM
User is offlineProfile CardPM
+Quote Post

vb5prgrmr

RE: VbOk & VbCancel Button Choices

23 Jun, 2009 - 04:31 AM
Post #5

D.I.C Regular
***

Joined: 21 Mar, 2009
Posts: 459



Thanked: 30 times
My Contributions
An inputbox returns a string so your Answer variable should either equal a vbNullString ("") or it should have a string contained within. So...
CODE

Answer = InputBox(...

If Answer = vbNullString Then
  'either user pressed cancel or user did not enter anything
Else
  'user entered something...
End If

However, since you enter a default value then perhaps the code should be ammended to...
CODE

Answer = InputBox(...

If Answer = vbNullString Then
  'user pressed cancel
ElseIf Answer = "EnterNameHere" then
  'user just pressed ok and did not enter name
else
  'user entered something...
End If



Good Luck



User is offlineProfile CardPM
+Quote Post

PNJLj

RE: VbOk & VbCancel Button Choices

29 Jun, 2009 - 10:24 PM
Post #6

New D.I.C Head
*

Joined: 17 May, 2009
Posts: 48



Thanked: 1 times
My Contributions
CODE
Private Sub cmdSave_Click()
    
    If lstYours.ListCount = -0 Then
        MsgBox "Cannot save or send as you have nothing in your list.", vbOKOnly + vbExclamation, "Sorry!"
    Else
        
        YourFile = InputBox("Please enter your name:" & vbCrLf & "Thanks!" & vbCrLf & "This will help me determine who's list I am looking at.", "FileName", "EnterNameHere")
        
        Select Case YourFile
        Case YourFile = ""
            MsgBox "Canceling", vbOKOnly, "Cancel!"
        Case YourFile = "EnterNameHere"
            MsgBox "Canceling", vbOKOnly, "Cancel!"
        Case Else
        
        Dim NextItem As Long
        Open "E:\" & YourFile & ".txt" For Append As #1
            ' Loop through the items in the listbox
            For NextItem = 0 To lstYours.ListCount - 1
                ' Write to the file the item text
                Print #1, lstYours.List(NextItem)
            Next
        Close #1
    
        ' Log into the E-Mail server
        EMailSession.SignOn
        EMailMessage.SessionID = EMailSession.SessionID
        ' Create the Message
        EMailMessage.Compose
        EMailMessage.RecipAddress = "j@hotmail.com"
        EMailMessage.RecipDisplayName = "l"
        EMailMessage.MsgSubject = "Here is " & YourFile & "'s list they sent."
        EMailMessage.MsgNoteText = "Hello l, here is my list." & vbCrLf & "Talk Soon!!"
        EMailMessage.AttachmentPathName = "E:\" & YourFile & ".txt"
        ' Send the Message
        EMailMessage.Send
        ' Log Off the E-Mail Server
        EMailSession.SignOff
        
        MsgBox "Your list has been sent to the Programmer (l)." & vbCrLf & "Thankyou!", vbOKOnly + vbExclamation, "E-Mail Sent!"
        
        End Select
    End If
    

End Sub


Okay, non of any of what you said worked so i thought i would try case select.
haveing difficulties thoe.
User is offlineProfile CardPM
+Quote Post

vb5prgrmr

RE: VbOk & VbCancel Button Choices

30 Jun, 2009 - 04:50 AM
Post #7

D.I.C Regular
***

Joined: 21 Mar, 2009
Posts: 459



Thanked: 30 times
My Contributions
Works for me...
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/7/09 10:02PM

Live VB Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

VB Tutorials

Reference Sheets

VB Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month