By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,781 Members | 1,134 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,781 IT Pros & Developers. It's quick & easy.

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

P: 12
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.)

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdCreate_Click()
  2.     strMessageBoxText = Text2.Text
  3.     strMessageBoxHeader = Text1.Text
  4.  
  5.     Debug.Print "Combo1.Text = "; Combo2.Text
  6.  
  7.  
  8.     If Combo1.Text = "Critical" Then
  9.         strAlertType = 16
  10.     ElseIf Combo1.Text = "Exclamation" Then
  11.         strAlertType = 48
  12.     ElseIf Combo1.Text = "Information" Then
  13.         strAlertType = 64
  14.     ElseIf Combo1.Text = "Question" Then
  15.         strAlertType = 32
  16.     End If
  17.  
  18.     If Combo2.Text = "Ok" Then
  19.         strButtonCombo = 0
  20.     ElseIf Combo2.Text = "Ok, Cancel" Then
  21.         strButtonCombo = 1
  22.     ElseIf Combo2.Text = "Yes, No" Then
  23.         strButtonCombo = 4
  24.     ElseIf Combo2.Text = "Yes, No, Cancel" Then
  25.         strButtonCombo = 3
  26.     ElseIf Combo2.Text = "Abort, Retry, Ignore" Then
  27.         strButtonCombo = 2
  28.     ElseIf Combo2.Text = "Retry, Cancel" Then
  29.         strButtonCombo = 5
  30.     End If
  31.  
  32.     strMessageBox = MsgBox(strMessageBoxText, strAlertType + strButtonCombo, strMessageBoxHeader)
  33.  
  34.     If strMessageBox = vbRetry Then
  35.         Do
  36.          strMessageBox = MsgBox(strMessageBoxText, strAlertType + strButtonCombo, strMessageBoxHeader)
  37.         Loop While strMessageBox <> vbAbort And strMessageBox <> vbIgnore And strMessageBox <> vbCancel
  38.     End If
  39.  
  40.  
  41.     strTest = MsgBox("Want to do a more advanced test?", vbQuestion + vbYesNo, "More Advanced Test")
  42.     If strTest = vbYes Then
  43.         frmMessageBoxCreator.Hide
  44.         strMessageBox = MsgBox(strMessageBoxText, strAlertType + strButtonCombo, strMessageBoxHeader)
  45.         If strMessageBox = vbRetry Then
  46.         Do
  47.          strMessageBox = MsgBox(strMessageBoxText, strAlertType + strButtonCombo, strMessageBoxHeader)
  48.         Loop While strMessageBox <> vbAbort And strMessageBox <> vbIgnore And strMessageBox <> vbCancel
  49.     End If
  50.         frmMessageBoxCreator.Show
  51.     End If
  52.     Debug.Print "Combo1.Text"; Combo2.Text
  53.  
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
Oct 29 '06 #1
Share this Question
Share on Google+
13 Replies


Expert 5K+
P: 8,434
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
    Expand|Select|Wrap|Line Numbers
    1. Private Sub cmdSave_Click()
    2. Open "Settings.txt" For Output Access Write Lock Read Write As #1
    3. Print #1, strMessageBoxText
    4. Print #1, strAlertType
    5. Print #1, strButtonCombo
    6. Print #1, strMessageBoxHeader
    7. Close #1
    8. 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:
    Expand|Select|Wrap|Line Numbers
    1. Private Sub Form_Load ()
    2. If Dir$("Settings.Txt") <> "" then  ' File exists, so read it...
    3.   Open "Settings.txt" For Input Access Read Shared As #1
    4.   Line Input #1, strMessageBoxText
    5.   Line Input #1, strAlertType
    6.   Line Input #1, strButtonCombo
    7.   Line Input #1, strMessageBoxHeader
    8.   Close #1
    9. End If
    10. End Sub
  • You could also save your settings in an INI file, or in the system registry.
Oct 30 '06 #2

P: 12
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.
Nov 1 '06 #3

Expert 5K+
P: 8,434
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.
Nov 1 '06 #4

P: 12
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
Nov 4 '06 #5

Expert 5K+
P: 8,434
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.
Nov 5 '06 #6

P: 12
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.
Nov 6 '06 #7

Expert 5K+
P: 8,434
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...
Expand|Select|Wrap|Line Numbers
  1. 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.
Nov 6 '06 #8

P: 12
If you want, you can also specify the full path. For instance...
Expand|Select|Wrap|Line Numbers
  1. 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:
Expand|Select|Wrap|Line Numbers
  1. or i = 0 To LstSave.ListCount - 1
  2.         A(i) = LstSave.List(i)
  3.         Next i
  4.         ItemCount = LstSave.ListCount
  5.         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
  6.         For i = 0 To ItemCount - 1
  7.             Write #1, A(i) 'write to the text file the contents of listbox
  8.             Next i
  9.             Close #1
  10.  
Any suggestions on how to save to the current exe directory?
Nov 17 '06 #9

P: 12
Sorry i copied the wrong code.

This is my current code
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Unload(Cancel As Integer)
  2.     Dim Save As String
  3. Open "\Ips.txt" For Output Access Write Lock Read Write As #1
  4.  
  5.         For i = 0 To List1.ListCount - 1
  6.         A(i) = List1.List(i)
  7.         Next i
  8.         ItemCount = List1.ListCount
  9.         For i = 0 To ItemCount - 1
  10.             Write #1, A(i) 'write to the text file the contents of listbox
  11.             Next i
  12.             Close #1
  13.  
Sry for the double post
Nov 17 '06 #10

Expert 5K+
P: 8,434
This might work
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Unload(Cancel As Integer)
  2.     Dim Save As String
  3.     Open App.Path &  "\Ips.txt" For Output Access Write Lock Read Write As #1
  4.     For i = 0 To List1.ListCount - 1
  5.         A(i) = List1.List(i)
  6.     Next i
  7.     ItemCount = List1.ListCount
  8.     For i = 0 To ItemCount - 1
  9.         Write #1, A(i) 'write to the text file the contents of listbox
  10.     Next i
  11.     Close #1
  12.  
Nov 17 '06 #11

P: 5
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
Nov 17 '06 #12

P: 12
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!

This might work
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Unload(Cancel As Integer)
  2.     Dim Save As String
  3.     Open App.Path &  "\Ips.txt" For Output Access Write Lock Read Write As #1
  4.     For i = 0 To List1.ListCount - 1
  5.         A(i) = List1.List(i)
  6.     Next i
  7.     ItemCount = List1.ListCount
  8.     For i = 0 To ItemCount - 1
  9.         Write #1, A(i) 'write to the text file the contents of listbox
  10.     Next i
  11.     Close #1
  12.  
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! ;)
Nov 18 '06 #13

Expert 5K+
P: 8,434
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.
Nov 19 '06 #14

Post your reply

Sign in to post your reply or Sign up for a free account.