Results 1 to 14 of 14

Thread: [SOLVED]"Variable not defined" error

  1. #1

    Thread Starter
    Addicted Member vigge89's Avatar
    Join Date
    May 2002
    Location
    Sweden
    Posts
    172

    [SOLVED]"Variable not defined" error

    I've set the variable "WhichFav" to the Index of a control, and when i execute this:
    VB Code:
    1. Call saveini("favorites", WhichFav)

    I get error saying: "Variable not defined" in the function saveini().

    Contents of saveini():
    VB Code:
    1. Private Function saveini(KeySection As String, KeyKey As String)
    2.  
    3. Dim lngResult As Long
    4. Dim strFileName
    5. strFileName = App.Path & "\options.ini"
    6. lngResult = WritePrivateProfileString(KeySection, _
    7. KeyKey, KeyValue, strFileName)
    8. If lngResult = 0 Then
    9. 'An error has occurred
    10. Call MsgBox("An error has occurred while saving the options", vbExclamation)
    11. End If
    12. KeyValue = Trim(strResult)
    13. End Function

    What is wrong?
    Could it be that WhichFav is an integer?
    I've tried CStr(WhichFav) but that doesn't work either, same error
    Last edited by vigge89; Dec 1st, 2003 at 01:19 PM.


  2. #2
    Frenzied Member axion_sa's Avatar
    Join Date
    Jan 2002
    Location
    Joburg, RSA
    Posts
    1,724
    Which line does VB break on?
    Where is KeyValue declared?

  3. #3

    Thread Starter
    Addicted Member vigge89's Avatar
    Join Date
    May 2002
    Location
    Sweden
    Posts
    172
    KeyValue is declared at top, so it's global.
    VB breaks at "KeyValue = Trim(strResult)"
    Where strResult is the undefined variable.

    It have worked before, but when I used WhichFav, it didn't work


  4. #4
    Frenzied Member axion_sa's Avatar
    Join Date
    Jan 2002
    Location
    Joburg, RSA
    Posts
    1,724
    And where is strResult declared?

  5. #5

    Thread Starter
    Addicted Member vigge89's Avatar
    Join Date
    May 2002
    Location
    Sweden
    Posts
    172
    nowhere?
    i don't know it worked before, but i'll test with declaring it as string


  6. #6

    Thread Starter
    Addicted Member vigge89's Avatar
    Join Date
    May 2002
    Location
    Sweden
    Posts
    172
    yep works now, declared the strResult as string (Dim strResult As String * 50)

    and added some stuff in
    VB Code:
    1. Private Sub mnuFavSet_Click()
    2.  
    3. lblFavColor(WhichFav).BackColor = lColor
    4.  
    5. KeyValue = lColor 'Added this, forgot it before (this makes the function write anything at all :P )
    6.  
    7. Call saveini("favorites", WhichFav)
    8.  
    9. End Sub


  7. #7
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    Originally posted by vigge89
    KeyValue is declared at top, so it's global.
    VB breaks at "KeyValue = Trim(strResult)"
    Where strResult is the undefined variable.

    It have worked before, but when I used WhichFav, it didn't work
    Unless strResult is defined somewhere within the scope of the function it will be "undefined".

    BTW, why is saveini a function since you are not returning anything from it.

  8. #8

    Thread Starter
    Addicted Member vigge89's Avatar
    Join Date
    May 2002
    Location
    Sweden
    Posts
    172
    Originally posted by MartinLiss
    Unless strResult is defined somewhere within the scope of the function it will be "undefined".

    BTW, why is saveini a function since you are not returning anything from it.
    since i don't know how to make the function return anything, i use a global variable to store its results

    also, i know that it has to be defined, but it worked when i used Saveini("favorites", lblSomecolor.Backcolor)
    Last edited by vigge89; Dec 1st, 2003 at 01:10 PM.


  9. #9
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    What do you want it to return? The following returns the result of the Trim function.

    VB Code:
    1. Private Function saveini(KeySection As String, KeyKey As String) [color="#0000FF"]As String[/color]
    2.  
    3. Dim lngResult As Long
    4. Dim strFileName
    5. strFileName = App.Path & "\options.ini"
    6. lngResult = WritePrivateProfileString(KeySection, _
    7. KeyKey, KeyValue, strFileName)
    8. If lngResult = 0 Then
    9. 'An error has occurred
    10. Call MsgBox("An error has occurred while saving the options", vbExclamation)
    11. End If
    12. [color="#FF00FF"]saveini [/color]= Trim(strResult)
    13. End Function

  10. #10

    Thread Starter
    Addicted Member vigge89's Avatar
    Join Date
    May 2002
    Location
    Sweden
    Posts
    172
    ok, great that was exactly what i wanted


  11. #11

  12. #12

    Thread Starter
    Addicted Member vigge89's Avatar
    Join Date
    May 2002
    Location
    Sweden
    Posts
    172
    I've already defined strResult as string!


  13. #13

    Thread Starter
    Addicted Member vigge89's Avatar
    Join Date
    May 2002
    Location
    Sweden
    Posts
    172
    New question:

    How do u convert a string into number?
    I've saved the xpos and ypos of a form in INI file, but when i retrieve them, i get type missmatch. Code:
    VB Code:
    1. Me.Left = Int(loadini("settings", "xpos"))
    2. Me.Top = Int(loadini("settings", "ypos"))

    I've tested with, and without the Int(), but it won't work


  14. #14

    Thread Starter
    Addicted Member vigge89's Avatar
    Join Date
    May 2002
    Location
    Sweden
    Posts
    172
    oh, nm, it worked now


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