Results 1 to 4 of 4
  1. #1
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Code: Function strGp(ByVal strSect As String, ByVa

    Friday March 2nd 2001 I am re-pasting upgraded code in here.


    >> Here's the partner to strPP which was posted yesterday (Feb19th 2001). strGP and strPP are the mainstay of control of values for most of my applications. I rely heavily on the INI files for guding the operation of a job.

    >> I am particularly fond of strGP because it allows me to specify a default value should one not be found.

    >> This means that an application can hold ALL its defaults embedded securely within the (locked) code. I stole this idea from MSWord - if you delete Normal.dot, Word grows a new one. So it is with my applications - if the INI file gets REALLY screwed up, the application just starts rebuilding those items as they are required.



    <pre>Public Function strGp(strFile As String, strSect As String, _
    strKey As String, strDefault As String) As String
    ' Procedure : strGp
    ' Description: This code returns a parameter from an INI file.
    ' Copyright: Chris Greaves Inc.
    ' Inputs: A file & section name, a key name, a default value (may be null)
    ' Returns: STRING.
    ' Assumes: Nothing
    ' Side Effects: None.
    ' Tested: By the calls shown below.
    ' First build a suitable file name.
    Dim strFileVal As String
    strFileVal = strSect
    ' Were we given only a local name? Test the Drive portion(=0)
    ' If local, build it in the host application's directory.
    If strBreakFileString(strFile, 0) = ":" Then
    strFileVal = MacroContainer.Path & Application.PathSeparator & strFile
    Else
    End If
    ' Is the INI extent absent? Test the extent portion.
    If strBreakFileString(strFile, 3) = "" Then
    strFileVal = strFile & ".INI"
    Else
    End If
    ' Now go to the INI file itself
    strGp = strPPS(strSect, strKey, strFileVal)
    ' If no data returned and we WERE given a default value, store that value and return it
    If strGp = "" Then
    Call strPP(strSect, strKey, strDefault, strFileVal)
    strGp = strDefault
    Else
    End If
    'Sub TESTstrGp()
    ' Should create a SECTION1.INI file in template's directory.
    'MsgBox strGp("eraseme","section 1", "key1", "default for key 1")
    ' Should create a SECTION2.INI file in template's directory.
    'MsgBox strGp("eraseme","section 2", "key2", "default for key 2")
    End Function
    </pre>


  2. #2
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Code: Function strGp(ByVal strSect As String, ByVa

    Chris,

    One thing I would find helpful with your routines is the meaning of the names. For instance, what does GP stand for in strGP?

    Maybe it's obvious, but I miss it. I like the idea here. Thanks for posting...
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  3. #3
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Code: Function strGp(ByVal strSect As String, ByVa

    Thanks for the feedback


    They were originally strGetParameter and strPutParameter but as I started to use them so much I got tired of typing the entire name, so i made them very VERY short.

    Your point is valid.

    I should consider placing the etymology of identifiers up with the descriptive text that heads the procedures.

    Was this procedure particularly awkward, or should i go back and revise others?

    How about the recent cmd_TypeEmail stuff. Did you find that readable?

  4. #4
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Code: Function strGp(ByVal strSect As String, ByVa

    Here's a stripped-down version of my strGP, strPP set of functions for maintaining an INI file.

    I realised on the weekend that analysing the file name (strBreakFileString etc) was a waste of time for me. In all my calls to strGP/strPP I use my compile-time string constant strcApplication, so it's always a local file name and I never supply the extent. Why not just let strGP add the extent without even testing?

    <pre>Option Explicit
    Declare Function apiGetPrivateProfileString Lib "kernel32" _
    Alias "GetPrivateProfileStringA" _
    (ByVal lpApplicationName As String, ByVal lpKeyName As Any, _
    ByVal lpDefault As String, ByVal lpReturnedString As String, _
    ByVal nSize As Long, ByVal lpFileName As String) As Long
    Declare Function apiPutPrivateProfileString Lib "kernel32" _
    Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, _
    ByVal lpKeyName As Any, ByVal lpValue As String, _
    ByVal lpFileName As String) As Long
    Public Const strcExtentSeparator As String = "."
    Public Const strcAllChars As String = "*"
    Public Function strGp(strFile As String, strSect As String, strKey As String, _
    strDefault As String) As String
    ' Procedure : strGp
    ' Description: This code returns a parameter from an INI file.
    ' Copyright: Chris Greaves Inc.
    ' Inputs: A file & section name, a key name, a default value (may be null)
    ' Returns: STRING.
    ' Assumes: Nothing
    ' Side Effects: None.
    ' Tested: By the calls shown below.
    ' First build a suitable file name.
    Dim strFileVal As String
    strFileVal = strFile & ".INI"
    ' Now go to the INI file itself
    strGp = strPPS(strSect, strKey, strFileVal)
    ' If no data returned and we WERE given a default value,
    ' store that value and return it
    If strGp = "" Then
    Call strPP(strFileVal, strSect, strKey, strDefault)
    strGp = strDefault
    Else
    End If
    'Sub TESTstrGp()
    ' Should create a eraseme.INI file in template's directory.
    'MsgBox strGp("eraseme","section 1", "key1", "default for key 1")
    ' Should create a eraseme.INI file in template's directory.
    'MsgBox strGp("eraseme","section 2", "key2", "default for key 2")
    'End Sub
    End Function
    Public Function strPP(strFile As String, strSect As String, strKey As String, _
    strValue As String) As String
    ' Procedure : strPP
    ' Description: This code sets a parameter in an INI file.
    ' Copyright: Chris Greaves Inc.
    ' Inputs: A file & section name, a key name, a value (may be null),
    ' a file name.
    ' Returns: STRING value of the parameter before it was replaced.
    ' Assumes: Nothing
    ' Side Effects: None.
    ' Tested: By the calls shown below.
    ' Were we given only a local name?
    Dim str As String
    str = strFile & ".INI"
    ' return the previous value, if it exists.
    strPP = strPPS(strSect, strKey, str)
    Call apiPutPrivateProfileString(strSect, strKey, strValue, str)
    'Sub TESTstrPP()
    ' Should create a SECTION3 in WOODY.INI file in template's directory.
    'MsgBox strPP("woody","section 3", "key3", "OLD value for key 3" )
    ' Should use a SECTION3 in WOODY.INI file in template's directory.
    'MsgBox strPP("woody", "section 3", "key3", "NEW value for key 3" )
    'End Sub
    End Function
    Public Function strPPS(strSect As String, strKey As String, strFile As String) _
    As String
    ' Procedure : strPPS
    ' Description: This code obtains a PrivateProfileString from an INI file.
    ' Copyright: Chris Greaves Inc.
    ' Inputs: A section name, a key name, a file name.
    ' Returns: STRING value of the parameter.
    ' Assumes: Nothing
    ' Side Effects: None.
    ' Tested: By the calls shown below.
    ' Were we given only a local name?
    Dim str As String
    str = strFile & ".INI"
    ' will hold the string (if any) from the INI file
    Dim strReturnedString As String
    ' availble space for string - aka the length of the buffer.
    Dim intSize As Integer
    ' count of characters actually returned.
    Dim intCharsReturned As Integer
    ' buffer set aside for the API call
    strReturnedString = Space$(255)
    ' To tell API how much space is available
    ' (API is too dumb to work it out for itself)
    intSize = Len(strReturnedString)
    intCharsReturned = apiGetPrivateProfileString(strSect, strKey, _
    "", strReturnedString, intSize, strFile)
    If intCharsReturned = 0 Then ' Didn't find the value
    strPPS = "" ' so return an empty string
    Else
    ' return only the significant characters
    strPPS = Left(strReturnedString, intCharsReturned)
    End If
    'Sub TESTstrPPS()
    '' Should NOT create a SECTION4.INI file in template's directory.
    'MsgBox strPPS("section4", "key4", "section4")
    'End Sub
    End Function
    </pre>


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •