Solved

Reading INI File key/value

Posted on 2004-09-02
4
355 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VBA Shell can't Find Word document 11 155
fso.FolderExists("\\server\HiddenFolder$") 4 97
Problem to line 23 80
how to loop through and process two columns in excel 8 72
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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
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…

739 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