How Do I Save Things With Vb 6 (At Runtime)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • HaggardSmurf
    New Member
    • Oct 2006
    • 12

    How Do I Save Things With Vb 6 (At Runtime)

    Ok, well ive made a simple little message box creator. What it does it lets you change the caption the header the buttons and the warning type of a message box. I now want to make it so if you want, you can save the message box youve just created. I have no idea how to do this, keep in mind im still a noob here is a sample of my code so you can see what level im at. (I would prefer it if you could show me how in terms that i would be able to understand.)

    Code:
    Private Sub cmdCreate_Click()
        strMessageBoxText = Text2.Text
        strMessageBoxHeader = Text1.Text
        
        Debug.Print "Combo1.Text = "; Combo2.Text
        
        
        If Combo1.Text = "Critical" Then
            strAlertType = 16
        ElseIf Combo1.Text = "Exclamation" Then
            strAlertType = 48
        ElseIf Combo1.Text = "Information" Then
            strAlertType = 64
        ElseIf Combo1.Text = "Question" Then
            strAlertType = 32
        End If
           
        If Combo2.Text = "Ok" Then
            strButtonCombo = 0
        ElseIf Combo2.Text = "Ok, Cancel" Then
            strButtonCombo = 1
        ElseIf Combo2.Text = "Yes, No" Then
            strButtonCombo = 4
        ElseIf Combo2.Text = "Yes, No, Cancel" Then
            strButtonCombo = 3
        ElseIf Combo2.Text = "Abort, Retry, Ignore" Then
            strButtonCombo = 2
        ElseIf Combo2.Text = "Retry, Cancel" Then
            strButtonCombo = 5
        End If
        
        strMessageBox = MsgBox(strMessageBoxText, strAlertType + strButtonCombo, strMessageBoxHeader)
        
        If strMessageBox = vbRetry Then
            Do
             strMessageBox = MsgBox(strMessageBoxText, strAlertType + strButtonCombo, strMessageBoxHeader)
            Loop While strMessageBox <> vbAbort And strMessageBox <> vbIgnore And strMessageBox <> vbCancel
        End If
        
            
        strTest = MsgBox("Want to do a more advanced test?", vbQuestion + vbYesNo, "More Advanced Test")
        If strTest = vbYes Then
            frmMessageBoxCreator.Hide
            strMessageBox = MsgBox(strMessageBoxText, strAlertType + strButtonCombo, strMessageBoxHeader)
            If strMessageBox = vbRetry Then
            Do
             strMessageBox = MsgBox(strMessageBoxText, strAlertType + strButtonCombo, strMessageBoxHeader)
            Loop While strMessageBox <> vbAbort And strMessageBox <> vbIgnore And strMessageBox <> vbCancel
        End If
            frmMessageBoxCreator.Show
        End If
        Debug.Print "Combo1.Text"; Combo2.Text
    Oh BTW ive named the 2 variables for alert type and button combos as "str..."
    thats because at first they were strings but then i changed the values to numbers instead of "vbCritical " or "vbYesNo"


    Thanks,

    HaggardSmurf
  • Killer42
    Recognized Expert Expert
    • Oct 2006
    • 8429

    #2
    Hi.

    Just a couple of pointers, that might be some help.
    • You can easily rename the variables by doing a Replace, the same as in Word, Excel or whatever. While in the code window, just press Ctrl-H or select Edit|Replace.
    • At a very simple level, you can write out your values to a text file and read them back in each time you start the program. For instance, something like
      Code:
      Private Sub cmdSave_Click()
      Open "Settings.txt" For Output Access Write Lock Read Write As #1
      Print #1, strMessageBoxText
      Print #1, strAlertType
      Print #1, strButtonCombo
      Print #1, strMessageBoxHeader
      Close #1
      End Sub
      Then at start-up (as an example I’ll use Form_Load procedure, but that may not fit what you’re doing) do this:
      Code:
      Private Sub Form_Load ()
      If Dir$("Settings.Txt") <> "" then  ' File exists, so read it...
        Open "Settings.txt" For Input Access Read Shared As #1
        Line Input #1, strMessageBoxText
        Line Input #1, strAlertType
        Line Input #1, strButtonCombo
        Line Input #1, strMessageBoxHeader
        Close #1
      End If
      End Sub
    • You could also save your settings in an INI file, or in the system registry.

    Comment

    • HaggardSmurf
      New Member
      • Oct 2006
      • 12

      #3
      So, I would make it log the settings then how would i make it so when it opens it will show the previous message box.

      Edit: I tried and it says Type Mismatch the variables are global and your variables are the same as i declared them.

      Comment

      • Killer42
        Recognized Expert Expert
        • Oct 2006
        • 8429

        #4
        Originally posted by HaggardSmurf
        So, I would make it log the settings then how would i make it so when it opens it will show the previous message box.

        Edit: I tried and it says Type Mismatch the variables are global and your variables are the same as i declared them.
        Well, the idea was that the proc I wrote (Form_Load in the example) would set up your variables the same way you had them set before. Then it's up to you to show your message box, the same way you are doing now, I suppose.

        Comment

        • HaggardSmurf
          New Member
          • Oct 2006
          • 12

          #5
          Originally posted by Killer42
          Well, the idea was that the proc I wrote (Form_Load in the example) would set up your variables the same way you had them set before. Then it's up to you to show your message box, the same way you are doing now, I suppose.
          Wait... What if i took your code and put it in a different exe's form load. Then that will load the previous settings. Would that work? If i did that how would i make the exe search wherever it is for the text document.

          Ex: I have a folder called messagebox creator. Both exe's are inside. I make a message box. I save the message box. I boot up the 2nd exe. How would it know to look in the folder that the 2nd exe itself is located in for the text document? Cause what i want to do is make it sendable so people can prank their friends. In order for it to be a good prank you need to be able to change the exe's name and folder name. No need to change the text document cause it will be invisible

          Comment

          • Killer42
            Recognized Expert Expert
            • Oct 2006
            • 8429

            #6
            Originally posted by HaggardSmurf
            Wait... What if i took your code and put it in a different exe's form load. Then that will load the previous settings. Would that work? If i did that how would i make the exe search wherever it is for the text document.

            Ex: I have a folder called messagebox creator. Both exe's are inside. I make a message box. I save the message box. I boot up the 2nd exe. How would it know to look in the folder that the 2nd exe itself is located in for the text document? Cause what i want to do is make it sendable so people can prank their friends. In order for it to be a good prank you need to be able to change the exe's name and folder name. No need to change the text document cause it will be invisible
            You might want to have a look in the VB doco for info on file access, specifically with regard to specifying the path. But the short version is, if you specify just the file name (Settings.txt in the example) then it will look in the "current" directory. That will usually be where the Exe was, but can change depending on how you do things. For instance, when you create a shortcut in Windows to run an Exe you can specify where to load it from, and what directory to run in.

            Comment

            • HaggardSmurf
              New Member
              • Oct 2006
              • 12

              #7
              Originally posted by Killer42
              You might want to have a look in the VB doco for info on file access, specifically with regard to specifying the path. But the short version is, if you specify just the file name (Settings.txt in the example) then it will look in the "current" directory. That will usually be where the Exe was, but can change depending on how you do things. For instance, when you create a shortcut in Windows to run an Exe you can specify where to load it from, and what directory to run in.

              Oh i see, thanks i'll try making it and see what happens.

              Comment

              • Killer42
                Recognized Expert Expert
                • Oct 2006
                • 8429

                #8
                Originally posted by HaggardSmurf
                Oh i see, thanks i'll try making it and see what happens.
                If you want, you can also specify the full path. For instance...
                Code:
                Open "C:\Temp\SomeFile.Txt" For Input Access Read Shared As #1
                This line is quite inflexible, of course, but you can produce all sorts of variations on the theme.

                Comment

                • HaggardSmurf
                  New Member
                  • Oct 2006
                  • 12

                  #9
                  Originally posted by Killer42
                  If you want, you can also specify the full path. For instance...
                  Code:
                  Open "C:\Temp\SomeFile.Txt" For Input Access Read Shared As #1
                  This line is quite inflexible, of course, but you can produce all sorts of variations on the theme.

                  Ok so ive found a different code that works and i seem to understand. Im on a new project now ;) im trying to save the contence of a list box this code works for me but is saving in the C:\ drive no directory which i wanted at first but now ive changed my mind and want it to save to the save folder as the exe. Ive tried saving to "\Ips.txt" and it didnt work.

                  *Btw this code is for an ip monitor it will monitor ip's trying to connect to you. My lame attempt at making a firewall :P

                  Anyways this is the code im using:
                  Code:
                  or i = 0 To LstSave.ListCount - 1
                          A(i) = LstSave.List(i)
                          Next i
                          ItemCount = LstSave.ListCount
                          Open "C:\Documents and Settings\YOUR USER DETAILS\Desktop\tempsave.txt" For Output As 1 'open the path to the text file to save data entries
                          For i = 0 To ItemCount - 1
                              Write #1, A(i) 'write to the text file the contents of listbox
                              Next i
                              Close #1
                  Any suggestions on how to save to the current exe directory?

                  Comment

                  • HaggardSmurf
                    New Member
                    • Oct 2006
                    • 12

                    #10
                    Sorry i copied the wrong code.

                    This is my current code
                    Code:
                    Private Sub Form_Unload(Cancel As Integer)
                        Dim Save As String
                    Open "\Ips.txt" For Output Access Write Lock Read Write As #1
                    
                            For i = 0 To List1.ListCount - 1
                            A(i) = List1.List(i)
                            Next i
                            ItemCount = List1.ListCount
                            For i = 0 To ItemCount - 1
                                Write #1, A(i) 'write to the text file the contents of listbox
                                Next i
                                Close #1
                    Sry for the double post

                    Comment

                    • Killer42
                      Recognized Expert Expert
                      • Oct 2006
                      • 8429

                      #11
                      This might work
                      Code:
                      Private Sub Form_Unload(Cancel As Integer)
                          Dim Save As String
                          Open [B]App.Path & [/B] "\Ips.txt" For Output Access Write Lock Read Write As #1
                          For i = 0 To List1.ListCount - 1
                              A(i) = List1.List(i)
                          Next i
                          ItemCount = List1.ListCount
                          For i = 0 To ItemCount - 1
                              Write #1, A(i) 'write to the text file the contents of listbox
                          Next i
                          Close #1

                      Comment

                      • schandraram
                        New Member
                        • Sep 2006
                        • 5

                        #12
                        Originally posted by HaggardSmurf
                        So, I would make it log the settings then how would i make it so when it opens it will show the previous message box.

                        Edit: I tried and it says Type Mismatch the variables are global and your variables are the same as i declared them.
                        I know its way too long after your post... but this might be helpful to you later too ;-)

                        The message box constant is a number whereas when you use the Line Input statement, you are reading in a string. Convert this string into a number and the code should work.

                        Hope this helps
                        Chandra

                        Comment

                        • HaggardSmurf
                          New Member
                          • Oct 2006
                          • 12

                          #13
                          Originally posted by schandraram
                          I know its way too long after your post... but this might be helpful to you later too ;-)

                          The message box constant is a number whereas when you use the Line Input statement, you are reading in a string. Convert this string into a number and the code should work.

                          Hope this helps
                          Chandra
                          Ohhh really? Cool i will try fixing that thank you!

                          Originally posted by Killer42
                          This might work
                          Code:
                          Private Sub Form_Unload(Cancel As Integer)
                              Dim Save As String
                              Open [B]App.Path & [/B] "\Ips.txt" For Output Access Write Lock Read Write As #1
                              For i = 0 To List1.ListCount - 1
                                  A(i) = List1.List(i)
                              Next i
                              ItemCount = List1.ListCount
                              For i = 0 To ItemCount - 1
                                  Write #1, A(i) 'write to the text file the contents of listbox
                              Next i
                              Close #1
                          Awsome! I thought just putting the file location as "/Ips.txt" would make it so the text doccument would be placed in the same folder. I never knew about the function app.path.

                          Tested it and it works Thanks! ;)

                          Comment

                          • Killer42
                            Recognized Expert Expert
                            • Oct 2006
                            • 8429

                            #14
                            Originally posted by HaggardSmurf
                            Awsome! I thought just putting the file location as "/Ips.txt" would make it so the text doccument would be placed in the same folder. I never knew about the function app.path.
                            Sounds as though you need to take the time to understand better how paths work. Starting with a backslash indicates the root directory of the current drive. You can specify the whole or partial path. In this case, the App object provides a number of useful pieces of information, including the .Path property. I can never remember whether this indicates the path where the Exe lives, or the one where you ran it. But more often that not, they're the same thing.

                            Ironically, you might have got the same end result by leaving out the backslash. If you had just specified "Ips.txt" with no path it would have used the current directory - which may or may not have been the correct one.

                            Comment

                            Working...