Results 1 to 27 of 27

Thread: Reading ini file [Resolved]

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Nov 2003
    Posts
    21

    Reading ini file [Resolved]

    hey
    I have looked over the forums and read lots of different ways to do this.. but to be honest i dont understand them at all.
    What i have is an ini file with:

    [List1]
    1=text
    2=text2
    3=text44
    to=from
    a=good

    ect ect

    and what i want to do is specify the number/word in the ini file, read it.. and return with the text after the "=" in a textbox (only on the one line of course lol). i know this is probably easy for some, and i think you can use api for it. anyway any help at all would be greatly appreciated as i dont understand reading ini files at all
    Last edited by Digit 0.1; Apr 6th, 2004 at 11:31 PM.

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    Here is an example.
    VB Code:
    1. 'Ini file contents...
    2. '[Settings]
    3. 'DBPath=C:\
    4.  
    5. Option Explicit
    6.  
    7. Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" _
    8. (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, _
    9. ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
    10.  
    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. Private Sub Command1_Click()
    15.  
    16.     Dim lngI As Long
    17.     Dim lngSize As Long
    18.     Dim strText255 As String * 255
    19.    
    20.     strText255 = ""
    21.     lngSize = Len(strText255)
    22.     lngI = GetPrivateProfileString("Settings", "DBPath", "", strText255, lngSize, "D:\Test.ini")
    23.     MsgBox Left$(strText255, lngI), vbOKOnly + vbInformation
    24.    
    25. End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Nov 2003
    Posts
    21
    ah that works great

    i understand how it works a bit more now lol.

    hmm.. is there a faster way of doing this? Cause the example words great but the ini file i have may get bigger and i think it may take longer to read..

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    If you break it into "sections" the size of the file will not matter.
    The "lpApplicationName" is the group, so if you have many small
    relevant groups the size will not matter.

    But if you have "[Settings]" and you have 100 keys below it then
    I would think it would affect it.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5
    Banned timeshifter's Avatar
    Join Date
    Mar 2004
    Location
    at my desk
    Posts
    2,465
    One of the games I am writing has an 8 page data file (*.txt) that I just use as a reference, but it's compressed, so I couldn't use it as an .ini file right off. Do you all think it would be a good idea for me to fully expand the file and use it as an .ini file? If so, I would need to know how to search for specific items to use in various parts of the program. It's another command I might like to have anyway..

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Nov 2003
    Posts
    21
    Originally posted by RobDog888
    If you break it into "sections" the size of the file will not matter.
    The "lpApplicationName" is the group, so if you have many small
    relevant groups the size will not matter.

    But if you have "[Settings]" and you have 100 keys below it then
    I would think it would affect it.
    ah.. then i have a problem cause thats exactly what i have, one section and like 200 keys damn. The annoying part is i cant break it down into smaller sections cause there all just one block.

    also, can i push my luck and ask for some help on writing over an existing key? I had a go at the write function but couldnt get it to write over.. any tiny examples would be great

    Edit: ok i did it, turns out i was tring to do it the same way as reading it and realised that was just plain stupid

    Wish there was a faster way to read it though
    Last edited by Digit 0.1; Mar 31st, 2004 at 06:59 PM.

  7. #7
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    If you can't break it up into relevant sections then try something like ...
    VB Code:
    1. [Section1]
    2. Key1=test1
    3. Key2=test2
    4. Key3=test3
    5.  
    6. [Section2]
    7. Key1=test1
    8. Key2=test2
    9. Key3=test3
    10.  
    11. [Section3]
    12. Key1=test1
    13. Key2=test2
    14. Key3=test3
    15. '...
    16. '...
    17. '...
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  8. #8
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    VB Code:
    1. WritePrivateProfileString "Settings", "DBPath", "Testing, "D:\Test.ini"
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Nov 2003
    Posts
    21
    Originally posted by RobDog888
    If you can't break it up into relevant sections then try something like ...
    VB Code:
    1. [Section1]
    2. Key1=test1
    3. Key2=test2
    4. Key3=test3
    5.  
    6. [Section2]
    7. Key1=test1
    8. Key2=test2
    9. Key3=test3
    10.  
    11. [Section3]
    12. Key1=test1
    13. Key2=test2
    14. Key3=test3
    15. '...
    16. '...
    17. '...
    hmm yea that might work, what i have is a list of (so-called) ai and when someone enters say "hello" it will say "hey" back.
    And what i do is basicly just read the whole ini for the "hello" string. hope that makes sence lol
    Originally posted by RobDog888
    WritePrivateProfileString "Settings", "DBPath", "Testing, "D:\Test.ini"
    thanks easier then reading it lol

  10. #10
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    "Sounds" like you would be better off using a database to store
    all of those AI responses. Would probably be faster also.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Nov 2003
    Posts
    21
    Originally posted by RobDog888
    "Sounds" like you would be better off using a database to store
    all of those AI responses. Would probably be faster also.
    yep, thats exactly what my freind said to me aswell.. but databases sound too complicated to set up, i think ini is ok for now, (and when i say for now i mean for the next few days lol)
    maybe i should look into making databases then

  12. #12
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    If you need any help designing the db post a new thread and we
    will try to help you. It's not as complicated as you think.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  13. #13

    Thread Starter
    Junior Member
    Join Date
    Nov 2003
    Posts
    21
    Originally posted by RobDog888
    If you need any help designing the db post a new thread and we
    will try to help you. It's not as complicated as you think.
    thanks i probably will once i feel i know as much as i can/want wiith ini reading and writing. i just want to get comfortable with them.

    I do have a slight problem which i cant seem to fix..
    when writing a 2 (or maybe more) digit number, it only writes the second one, i would do for example:
    Code:
    'amount changes, lets just say num is 1 and amount is 50 (should add to 51)
    Dim Num as string
    Num = Num + (amount)
    msgbox num 'to make sure it adds correctly and it does
    WritePrivateProfileString "settings", "num", (Num), App.Path & "\data\" & name & ".ini"
    it seems to write fine.. but when i open the num again, it only recorded and wrote the last number "0" This is annoying as i cant figure out whats wrong, can anyone help?

  14. #14
    Frenzied Member
    Join Date
    May 2003
    Location
    So Cal
    Posts
    1,564
    Remove the ( ) around Num, not sure if that's what causing it, but you dont need them around the Variable.

    WritePrivateProfileString "settings", "num", Num, App.Path & "\data\" & name & ".ini"

  15. #15
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    Since Num is a string try...
    VB Code:
    1. Num = Num & amount
    But since these are actually numeric values it will not add the way you want.

    It will result in "150". Change the Num and amount to Integer or
    Longs. Then ...
    VB Code:
    1. Num = Num + amount ' = 51
    But then when you write to the ini file use the CStr() function to
    change them to strings so the API can write to the ini file or you
    will get a type mismatch error.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  16. #16

    Thread Starter
    Junior Member
    Join Date
    Nov 2003
    Posts
    21
    Originally posted by BrianS
    Remove the ( ) around Num, not sure if that's what causing it, but you dont need them around the Variable.

    WritePrivateProfileString "settings", "num", Num, App.Path & "\data\" & name & ".ini"
    oh ok, thanks hmm didnt fix the problem though
    Originally posted by RobDog888
    Since Num is a string try...
    VB Code:
    1. Num = Num & amount
    But since these are actually numeric values it will not add the way you want.

    It will result in "150". Change the Num and amount to Integer or
    Longs. Then ...
    VB Code:
    1. Num = Num + amount ' = 51
    But then when you write to the ini file use the CStr() function to
    change them to strings so the API can write to the ini file or you
    will get a type mismatch error.
    i think im doing something wrong, i tried chaning both the ammount and num to integer and longs.. but when i do this vb crashes.. my code:
    Code:
    Private Function GiveCredits(ammount As String, email As String)
    On Error Resume Next
    Dim CredTot As Long
    Dim FullCred As String
            CredTot = CreditsCount(email)
            MsgBox CredTot, , "credtot"
        If CredTot <= "0" Then
            WritePrivateProfileString "settings", "credits", (ammount), App.Path & "\userdata\" & email & ".ini"
        Else
            MsgBox ammount, , "ammount"
           FullCred = CredTot + (ammount)
           MsgBox FullCred, , "fullcred"
           WritePrivateProfileString "settings", "credits", FullCred, App.Path & "\userdata\" & email & ".ini"
        End If
    End Function
    ive probably made a mistake somewhere or done something wrong.. but i cant see it
    But then when you write to the ini file use the CStr() function to
    change them to strings so the API can write to the ini file or you
    will get a type mismatch error
    umm, sorry im not sure how to do this, could you elaborate please

  17. #17
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    What is CreditsCount? Try stepping through your code one line at
    a time to find the offending line of code.

    CStr:
    VB Code:
    1. WritePrivateProfileString "settings", "credits", CStr(ammount), App.Path & "\userdata\" & email & ".ini"
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  18. #18

    Thread Starter
    Junior Member
    Join Date
    Nov 2003
    Posts
    21
    oh.. credits count is to find the current ammount and that is:
    Code:
    Private Function CreditsCount(UserEmail As String)
    Dim Cred As String
    Dim lngI As Long
    Dim lngSize As Long
    Dim strText255 As String * 255
            strText255 = ""
            lngSize = Len(strText255)
            lngI = GetPrivateProfileString("settings", "credits", "", strText255, lngSize, App.Path & "\userdata\" & UserEmail & ".ini")
            Cred = Left$(strText255, lngI)
        CreditsCount = Cred
    End Function
    Originally posted by RobDog888
    CStr:
    VB Code:
    1. WritePrivateProfileString "settings", "credits", CStr(ammount), App.Path & "\userdata\" & email & ".ini"
    ooh ok, thanks

  19. #19
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    Did that solve your issue?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  20. #20

    Thread Starter
    Junior Member
    Join Date
    Nov 2003
    Posts
    21
    well it solved the crashing when i tried it.. but it still gives me only the second digit, current code is:
    Code:
    Private Function GiveCredits(ammount As String, email As String)
    On Error Resume Next
    Dim CredTot As Long
    Dim FullCred As Long
            CredTot = CreditsCount(email)
            MsgBox CredTot, , "credtot"
        If CredTot <= "0" Then
            WritePrivateProfileString "settings", "credits", CStr(ammount), App.Path & "\userdata\" & email & ".ini"
        Else
            MsgBox ammount, , "ammount"
           FullCred = CredTot + (ammount)
           MsgBox FullCred, , "fullcred"
           WritePrivateProfileString "settings", "credits", CStr(FullCred), App.Path & "\userdata\" & email & ".ini"
        End If
    End Function
    where i have ammount As String, maybe that should be ammount as integer?

  21. #21
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    The problem is in the function that generates the Num value.
    Can you post it?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  22. #22

    Thread Starter
    Junior Member
    Join Date
    Nov 2003
    Posts
    21
    sure, this is what i have:
    Code:
    'game part
    Private Function GuessNum(Player As String, nGuess As String)
    Dim Guess As Long
    Dim Roll As Long
            Guess = nGuess
            Roll = Int(Rnd * 10)
            'MsgBox Guess
            'MsgBox Roll
            If Guess = Roll Then
                GiveCredits "50", Player
            End If
    End Function
    
    'give the credits part
    Private Function GiveCredits(ammount As Integer, email As String)
    On Error Resume Next
    Dim CredTot As Long
    Dim FullCred As Long
            CredTot = CreditsCount(email)
            MsgBox CredTot, , "credtot"
        If CredTot <= "0" Then
            WritePrivateProfileString "settings", "credits", CStr(ammount), App.Path & "\userdata\" & email & ".ini"
        Else
            MsgBox ammount, , "ammount"
           FullCred = CredTot + (ammount)
           MsgBox FullCred, , "fullcred"
           WritePrivateProfileString "settings", "credits", CStr(FullCred), App.Path & "\userdata\" & email & ".ini"
        End If
    End Function
    
    'check current credits
    Private Function CreditsCount(UserEmail As String)
    Dim Cred As String
    Dim lngI As Long
    Dim lngSize As Long
    Dim strText255 As String * 255
            strText255 = ""
            lngSize = Len(strText255)
            lngI = GetPrivateProfileString("settings", "credits", "", strText255, lngSize, App.Path & "\userdata\" & UserEmail & ".ini")
            Cred = Left$(strText255, lngI)
        CreditsCount = Cred
    End Function
    hope that makes sence

  23. #23
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    Worked fine for me when I made this change
    VB Code:
    1. GiveCredits "50", CStr(Player)
    This is what I did to get the example to run
    GuessNum_Click is a command button
    VB Code:
    1. 'Option Explicit
    2.  
    3. Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" _
    4. (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, _
    5. ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
    6.  
    7. Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" _
    8. (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long
    9.  
    10. Private Sub Form_Load()
    11.     Player = "RobDog888"
    12. End Sub
    13.  
    14. Private Sub GuessNum_Click()
    15. Player = "RobDog888"
    16. nGuess = 1
    17. Dim Guess As Long
    18. Dim Roll As Long
    19.         Guess = nGuess
    20.         Roll = Int(Rnd * 10)
    21.         'MsgBox Guess
    22.         'MsgBox Roll
    23.         If Guess = Roll Then
    24.             GiveCredits "50", CStr(Player)
    25.         Else
    26.             MsgBox "Wrong Guess!"
    27.         End If
    28. End Sub
    29.  
    30. 'give the credits part
    31. Private Function GiveCredits(ammount As Integer, email As String)
    32. On Error Resume Next
    33. Dim CredTot As Long
    34. Dim FullCred As Long
    35.         CredTot = CreditsCount(email)
    36.         MsgBox CredTot, , "credtot"
    37.     If CredTot <= "0" Then
    38.         WritePrivateProfileString "settings", "credits", CStr(ammount), App.Path & "\userdata\" & email & ".ini"
    39.     Else
    40.         MsgBox ammount, , "ammount"
    41.        FullCred = CredTot + (ammount)
    42.        MsgBox FullCred, , "fullcred"
    43.        WritePrivateProfileString "settings", "credits", CStr(FullCred), App.Path & "\userdata\" & email & ".ini"
    44.     End If
    45. End Function
    46.  
    47. 'check current credits
    48. Private Function CreditsCount(UserEmail As String)
    49. Dim Cred As String
    50. Dim lngI As Long
    51. Dim lngSize As Long
    52. Dim strText255 As String * 255
    53.         strText255 = ""
    54.         lngSize = Len(strText255)
    55.         lngI = GetPrivateProfileString("settings", "credits", "", strText255, lngSize, App.Path & "\userdata\" & UserEmail & ".ini")
    56.         Cred = Left$(strText255, lngI)
    57.     CreditsCount = Cred
    58. End Function
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  24. #24

    Thread Starter
    Junior Member
    Join Date
    Nov 2003
    Posts
    21
    Originally posted by RobDog888
    Worked fine for me when I made this change
    VB Code:
    1. GiveCredits "50", CStr(Player)
    This is what I did to get the example to run
    GuessNum_Click is a command button
    VB Code:
    1. 'Option Explicit
    2.  
    3. Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" _
    4. (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, _
    5. ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
    6.  
    7. Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" _
    8. (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long
    9.  
    10. Private Sub Form_Load()
    11.     Player = "RobDog888"
    12. End Sub
    13.  
    14. Private Sub GuessNum_Click()
    15. Player = "RobDog888"
    16. nGuess = 1
    17. Dim Guess As Long
    18. Dim Roll As Long
    19.         Guess = nGuess
    20.         Roll = Int(Rnd * 10)
    21.         'MsgBox Guess
    22.         'MsgBox Roll
    23.         If Guess = Roll Then
    24.             GiveCredits "50", CStr(Player)
    25.         Else
    26.             MsgBox "Wrong Guess!"
    27.         End If
    28. End Sub
    29.  
    30. 'give the credits part
    31. Private Function GiveCredits(ammount As Integer, email As String)
    32. On Error Resume Next
    33. Dim CredTot As Long
    34. Dim FullCred As Long
    35.         CredTot = CreditsCount(email)
    36.         MsgBox CredTot, , "credtot"
    37.     If CredTot <= "0" Then
    38.         WritePrivateProfileString "settings", "credits", CStr(ammount), App.Path & "\userdata\" & email & ".ini"
    39.     Else
    40.         MsgBox ammount, , "ammount"
    41.        FullCred = CredTot + (ammount)
    42.        MsgBox FullCred, , "fullcred"
    43.        WritePrivateProfileString "settings", "credits", CStr(FullCred), App.Path & "\userdata\" & email & ".ini"
    44.     End If
    45. End Function
    46.  
    47. 'check current credits
    48. Private Function CreditsCount(UserEmail As String)
    49. Dim Cred As String
    50. Dim lngI As Long
    51. Dim lngSize As Long
    52. Dim strText255 As String * 255
    53.         strText255 = ""
    54.         lngSize = Len(strText255)
    55.         lngI = GetPrivateProfileString("settings", "credits", "", strText255, lngSize, App.Path & "\userdata\" & UserEmail & ".ini")
    56.         Cred = Left$(strText255, lngI)
    57.     CreditsCount = Cred
    58. End Function
    i tried this in a new project and when i win, i always get a msgbox with "0" and doesnt write the ini.. so i dont think thats working for me either
    i also tried changing the:
    GiveCredits "50", (player) to
    GiveCredits "50", CStr(Player
    and still keep getting the single digit at the end.. this is annoying lol, whats even worse is i dont know what im doing wrong, and it seems to work fine for you lol
    if i could get your code working, would i be able to do it then with different players and guesses cause thats what i need

  25. #25
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    Step through my code by pressing F8 once for each line you want
    to advance. Check the values from the CreditsCount procedure.
    I have a feeling that it is only taking a lenght of one from there.
    Also, the length of UserEmail is ? in the CreditsCount procedure?

    What OS and version are you running?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  26. #26

    Thread Starter
    Junior Member
    Join Date
    Nov 2003
    Posts
    21
    I went though my entire source last night and found what was wrong finally, what i didn’t see was that it actually 'did' write the correct value. But i had actually called the same function again in another part where i was first testing this, and it was writing.. then re-writing back again..
    anyway, i understand it a whole lot better now

    Thanks RobDog888! Really appreciate all the help!
    i know where to come if i need more help in the future lol
    (which i will)

  27. #27
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    Glad to hear that you got it "[Resolved]" and that you are learning!

    Always here to help.

    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

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