Solved

Reading INI File key/value

Posted on 2004-09-02
4
339 Views
Last Modified: 2013-12-25
[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
Comment
Question by:zachvaldez
  • 2
4 Comments
 
LVL 29

Assisted Solution

by:leonstryker
leonstryker earned 50 total points
ID: 11968466
>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
 
LVL 5

Accepted Solution

by:
KarcOrigin earned 450 total points
ID: 11969695
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
 

Author Comment

by:zachvaldez
ID: 11973610
KarcOrigin,

THAT WAS AN ABSOLUTE BRILLIANCY!!! Additional points added for you!! That code was amazing yet simple!
0
 
LVL 5

Expert Comment

by:KarcOrigin
ID: 11979592
Thanks zachvaldez I am happy that I have helped someone like you. Keep it up!

Cheers!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question