Solved

Reading INI File key/value

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

707 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

12 Experts available now in Live!

Get 1:1 Help Now