Results 1 to 17 of 17

Thread: writing ini file

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2005
    Posts
    277

    writing ini file

    Hi all again.

    does VB have the capapbilities to be able to use textboxes that user can add information and then click a button and this will write a .ini file. for example can I use text boxes for the user to add their information and then this will update the .ini file

    Name=Server 1
    Phonetic=Server 1
    Comment=Hello
    Port=3784
    Auth=0
    Duplicates=1
    AdminPassword=
    Password=
    MaxClients=32

    like if I had text b oxes for each of these - could the user input their information and will VB write this to the .ini file.

    Thanks again
    Rob

  2. #2
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: writing ini file

    If you do a search of the forums for WritePrivateProfileString you'll find a lot of examples of writing and reading ini files. But ini files are old-fashioned. If you want to use the Registry then look at the example in my signature.

  3. #3

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2005
    Posts
    277

    Re: writing ini file

    Sorry Rhinobull if you misunderstood me, what I am trying to acheive is to have a number of text boxes that the user can insert info and then have a cmd button that will add this to thi ini file - if you get me.

    thanks in advance
    ?R

  5. #5
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: writing ini file

    Start a new project, put three textboxes on a form and then add this code and see if that's what you want.


    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.  
    5.     ' The "" is the default value for the textboxes - in other words
    6.     ' the first time the app is run.
    7.     Text1.Text = GetSetting(App.EXEName, "My Textboxes", "Textbox 1", "")
    8.     Text2.Text = GetSetting(App.EXEName, "My Textboxes", "Textbox 2", "")
    9.     Text3.Text = GetSetting(App.EXEName, "My Textboxes", "Textbox 3", "")
    10.    
    11. End Sub
    12.  
    13.  
    14. Private Sub Form_Unload(Cancel As Integer)
    15.  
    16.     SaveSetting App.EXEName, "My Textboxes", "Textbox 1", Text1.Text
    17.     SaveSetting App.EXEName, "My Textboxes", "Textbox 2", Text2.Text
    18.     SaveSetting App.EXEName, "My Textboxes", "Textbox 3", Text3.Text
    19.    
    20. End Sub

  6. #6
    Addicted Member
    Join Date
    May 2004
    Location
    Nagpur, India
    Posts
    228

    Re: writing ini file

    Just a word of caution, even though INI is old stuff and Registry is the place where information is stored now a days, you need to keep in mind following things:
    1. Whether there will be multiple Windows users using your program.
    2. Where to save in registry based on above.
    3. All users dont have access to Write to HKLM.
    4. Every user has his own HKCU, so settings become user specific here.

    All this above details may not be relevent to your post 100%, but you should also consider this.
    If there is 1 setting per computer, then you better save it to INI and place this INI in "All Users" folder so that everybody can write to it.

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2005
    Posts
    277

    Re: writing ini file

    Im not too sure how that example would work. What I was hoping to do was to have all the fields required as text boxes and then when the user has entered their information they click a button and it will write/update a named ini file

    hope
    you understand thanks
    R

  8. #8
    Fanatic Member doofusboy's Avatar
    Join Date
    Apr 2003
    Posts
    526

    Re: writing ini file

    What's wrong with using:

    VB Code:
    1. Private Sub Command1_Click()
    2. Open App.Path & "\my_ini.ini" For Output As #1
    3. Print #1, "[Info from 1st textbox]"
    4. Print #1, "Setting #1 ="; Text1(0).Text
    5. Print #1, "[Info from 2nd textbox]"
    6. Print #1, "Setting #2 ="; Text1(1).Text
    7. Print #1, "[Info from 1st textbox]"
    8. Print #1, "Setting #3 ="; Text1(2).Text
    9.  
    10. End Sub
    Last edited by doofusboy; Sep 1st, 2005 at 09:10 AM.
    Do canibals not eat clowns because they taste funny?

  9. #9
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: writing ini file

    Quote Originally Posted by robvr6
    Sorry Rhinobull if you misunderstood me, ...
    I did undesrtand what you want so I sent you a sample project which instead of saving some text saves form's left/top ... What you need is to take alook at it one more time and [b]try]/b] to modify it. Other than that you have everything.

  10. #10
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: writing ini file

    Quote Originally Posted by doofusboy
    What's wrong with using:

    VB Code:
    1. Private Sub Command1_Click()
    2. Open App.Path & "\my_ini.ini" For Output As #1
    3. Print #1, "[Info from 1st textbox]"
    4. Print #1, "Setting #1 ="; Text1(0).Text
    5. '...
    6. End Sub
    Nothing wrong with that except that you will ahve a hard time finding some particular entry and if you use few APIs instead then you'll have no problem what-so-ever.

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2005
    Posts
    277

    Re: writing ini file

    Right I got the file writing OK using the method doofusboy explained because I understood that more. One question though is there a way that I can have a check box next to some of the text boxes so that if the field isnt required it can be hashed out.

    i.e so it will originally display port=3784 and when checked this # port=3784

    hope someone can help.

    Thanks
    R

  12. #12
    Addicted Member
    Join Date
    Jul 2005
    Posts
    158

    Re: writing ini file

    can u do the checkboxes like...
    VB Code:
    1. SaveSetting App.EXEName, "My Checkbox", "check1", Check1.Value
    2. &
    3. Check1.Value = GetSetting(App.EXEName, "My Checkbox", "check1", "")
    ???
    Last edited by Ch4s3t0ph3r; Dec 10th, 2006 at 01:14 PM.

  13. #13
    Addicted Member
    Join Date
    Aug 2002
    Location
    Alberta, Canada
    Posts
    138

    Re: writing ini file

    I have been using .ini files ever since the first time I tried to use the registry

    While ini files are "out of date" I think they are the best solution because they are easy to use, and alot of programers these days just dont seem to care if their registry entries get deleted when their program is uninstalled, causing all sorts of annoyances and problems for your users computer.

    That is however just my opinion.

    Here is a module I have been using for the last 3 or 4 years

    VB Code:
    1. 'Put this in a module
    2.  
    3. '--------for INI file read/write
    4. Private Declare Function GetPrivateProfileSection Lib "kernel32" Alias "GetPrivateProfileSectionA"  _
    5. (ByVal lpAppName As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
    6. Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" _  
    7. (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String,  _
    8. ByVal nSize As Long, ByVal lpFileName As String) As Long
    9. Private Declare Function WritePrivateProfileSection Lib "kernel32" Alias "WritePrivateProfileSectionA"  _
    10. (ByVal lpAppName As String, ByVal lpString As String, ByVal lpFileName As String) As Long
    11. Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" _
    12. (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long
    13. '-------------------
    14.  
    15. 'reads ini string
    16. Public Function ReadIni(filename As String, Section As String, Key As String) As String
    17. Dim RetVal As String * 255, v As Long
    18. v = GetPrivateProfileString(Section, Key, "", RetVal, 255, filename)
    19. ReadIni = left(RetVal, v)
    20. End Function
    21.  
    22. 'reads ini section
    23. Public Function ReadIniSection(filename As String, Section As String) As String
    24. Dim RetVal As String * 255, v As Long
    25. v = GetPrivateProfileSection(Section, RetVal, 255, filename)
    26. ReadIniSection = left(RetVal, v - 1)
    27. End Function
    28.  
    29. 'writes ini
    30. Public Sub WriteIni(filename As String, Section As String, Key As String, Value As String)
    31. WritePrivateProfileString Section, Key, Value, filename
    32. End Sub
    33.  
    34. 'writes ini section
    35. Public Sub WriteIniSection(filename As String, Section As String, Value As String)
    36. WritePrivateProfileSection Section, Value, filename
    37. End Sub

    Im sorry to whoever wrote that module, I don't remember who I got it from

    Play around with it for a while and you might find that you like it .
    Jon_G
    AKA Phil From Marketing
    **************************************
    Insert Something Memorable And Witty Here.
    **************************************

  14. #14
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: writing ini file

    Quote Originally Posted by Ch4s3t0ph3r
    can u do the checkboxes like...
    VB Code:
    1. SaveSetting App.EXEName, "My Checkbox", "check1", Check1.Value
    2. &
    3. Check1.Value = GetSetting(App.EXEName, "My Checkbox", "check1", "")
    ???
    Yes but with one small change. The 4th parameter of GetSetting is the default value that is used if VB can not find any data. That would happen for example the first time your program runs. Since you are trying to get the value for a checkbox you want to use either one of the three built-in constants for the checkbox value which are vbChecked, vbUnchecked and vbGrayed and so if you want the checkbox to be unchecked the first time the program runs your GetSetting should look like

    Check1.Value = GetSetting(App.EXEName, "My Checkbox", "check1", vbUnChecked)

  15. #15
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Re: writing ini file

    I need to save certian information that needs to be available to different users on the same computer such as Database type (MySQL, MS SQL etc.), the IP Address of the server etc. Would I be better off using an INI File or saving the data to the registry? Is it possible to save data into another part of the registry other than in the HKEY_CURRENT_USER|Software|VB and VBA Program Settings?

    Will the module post in this LINK allow me to save and read different registry sections?

    Thanks!
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


  16. #16
    Addicted Member
    Join Date
    Jul 2005
    Posts
    158

    Re: writing ini file

    Quote Originally Posted by doofusboy
    What's wrong with using:

    VB Code:
    1. Private Sub Command1_Click()
    2. Open App.Path & "\my_ini.ini" For Output As #1
    3. Print #1, "[Info from 1st textbox]"
    4. Print #1, "Setting #1 ="; Text1(0).Text
    5. Print #1, "[Info from 2nd textbox]"
    6. Print #1, "Setting #2 ="; Text1(1).Text
    7. Print #1, "[Info from 1st textbox]"
    8. Print #1, "Setting #3 ="; Text1(2).Text
    9.  
    10. End Sub
    how would you go about opening this info into textboxes?
    Last edited by Ch4s3t0ph3r; Dec 10th, 2006 at 01:14 PM.

  17. #17
    Fanatic Member doofusboy's Avatar
    Join Date
    Apr 2003
    Posts
    526

    Re: writing ini file

    Continuing with previous example:
    VB Code:
    1. ' Create variable to hold data read from ini file
    2. Dim read_data As String
    3. ' Open ini file for reading
    4. Open App.Path & "\my_ini.ini" For Input As #1
    5. While Not EOF(1)
    6.     ' Read file line by line
    7.     Line Input #1, read_data
    8.     Line Input #1, read_data
    9.     ' Two lines are read, because we only want data from every other line
    10.     ' Put data after '=' into textbox
    11.     Text1(i).Text = Split(read_data, "=")(1)
    12. Wend
    13. ' Close input file
    14. Close #1

    If you write more than just textbox data to the ini file, you'd obviously need to run some sort of check to determine whether the data read is for a checkbox, textbox, or whatever and code accordingly.
    Do canibals not eat clowns because they taste funny?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width