Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 397
  • Last Modified:

Reading INI File key/value

[ACCESS]
ID=632992
ID=555555
ID=999999
ID=888888

Above is an ini file. The logic I am trying to build is to check if your ID is listed here. If yes, you have access to a form/button otherwise I'll leave the control disabled or locked.

How is that coded?
0
zachvaldez
Asked:
zachvaldez
  • 2
2 Solutions
 
leonstrykerCommented:
>How is that coded?

Sorry, but that is incorrect.  An ini file rellies on the fact that each item has a unique tag, so you can not have multiple items with a tag id.  

What you may wish to do is store the values in a plain text file. you may then access this file and check if a certain value is stored in the file.  There are several technques to this.  One is to access the text file with ADO and run SQL queries.  A different way would be to read the values into an array and then test against that.

How much data are you planning to store in the file?  Have you also considered using Excel and/or a database table for this?

Leon
0
 
KarcOriginCommented:
Try this:

Private Sub Command1_Click()
    If (isKeyFound("[Access]", "ID", "9992999") = True) Then
        ' Enable your controls here
        Debug.Print "Key found"
    Else
        ' Disable your controls here
        Debug.Print "Key not found"
    End If
   
End Sub

Private Function isKeyFound(ByVal section As String, ByVal key As String, ByVal searchKey As String) As Boolean

    Dim myFileArray() As String, myLine As String, cLine As Integer, revLine As Integer
   
    'read all lines from file to array
    Open "c:\My_Space\test.INI" For Input As #1
    Do Until EOF(1)
        Line Input #1, myLine
        ReDim Preserve myFileArray(cLine)
        myFileArray(cLine) = myLine
        cLine = cLine + 1
    Loop
    Close #1
   
    Dim k As Integer
    Dim i As Integer
    Dim j As Integer
   
    ' Loop through all the lines
    For i = 0 To (cLine - 1)
   
        ' Check for the section in the INI file
        If (section = myFileArray(i)) Then
       
            ' Once the section found then check for the perticular key under this section
            For j = i To (cLine - 1)
           
                ' If the key has some value then only go into
                k = InStr(1, myFileArray(j), "=", vbTextCompare)
                If (k > 0) Then
               
                    ' Check the key name matches the key for which you want to find the value
                    If (key = Mid(myFileArray(j), 1, k - 1)) Then
                   
                        ' If the key matches then match the value of this key with your value
                        If (searchKey = Mid(myFileArray(j), k + 1)) Then
                            isKeyFound = True
                            Exit Function
                        End If
                    End If
                End If
            Next
            i = j
        End If
    Next
   
    isKeyFound = False
   
End Function

I hope it will helps you.

Cheers
0
 
zachvaldezAuthor Commented:
KarcOrigin,

THAT WAS AN ABSOLUTE BRILLIANCY!!! Additional points added for you!! That code was amazing yet simple!
0
 
KarcOriginCommented:
Thanks zachvaldez I am happy that I have helped someone like you. Keep it up!

Cheers!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now