Solved

Reading INI File key/value

Posted on 2004-09-02
4
316 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

910 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now