Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to read the contents of a Windows register key 1

Status
Not open for further replies.

Werneck

Technical User
Aug 5, 2003
36
0
0
BR
Hi

How do I get the string of the following register section, in Acc97 vba code? Is there a simple code for this like using the Environ()?

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User

Thanks
 
Hi Werneck

Check your VBA help for the
Code:
System.PrivateProfileString
command. I've used it in Office 2000 & 2002, but I'm not sure if it exists in '97.

Regards

Mac
 
API call should work:
Code:
Private Declare Function WNetGetUser Lib "mpr.dll" Alias "WNetGetUserA" (ByVal lpName As String, ByVal lpUserName As String, lpnLength As Long) As Long


Public Function UserLoginName() As String

  Dim strName As String
  strName = Space(255)
  
  Call WNetGetUser("", strName, Len(strName))
  
  UserLoginName = Trim(strName)
  
End Function

VBSlammer
redinvader3walking.gif

Unemployed in Houston, Texas
 

I could not find VBA help for the System.PrivateProfileString command as referenced!

Can some one point it more precisely to me.

VBslammer's alternative seems simple, but I'm trying to avoid API calls.

Thank you
Werneck
 
Okay, have I missed the point here and you just want
Code:
Environ("username")
?
 

jrbarnett said: it is all to easy to map the my documents directory to be outside the user profile in most versions of Windows. Instead, I would read the Personal key from within the following registry section:

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\Personal
which contains the string: %USERPROFILE%\My documents

That's why I'm trying to get a procedure to read the registry or preferably an easy function analog to the environ().

Does your toolbox has it?
Werneck
 
I don't see why you object to using the API. There are so many variables to consider when trying to retrieve this info from different systems that you need the level of abstraction provided by the API to eliminate complexity.

There are a number of articles that address this issue, such as this excellent one:


The solution defined in the article follows:
Code:
'@------------- API -----------------@

Public Declare Function SHGetSpecialFolderLocation _
    Lib "shell32" (ByVal hWnd As Long, _
    ByVal nFolder As Long, ppidl As Long) As Long

Public Declare Function SHGetPathFromIDList _
    Lib "shell32" Alias "SHGetPathFromIDListA" _
    (ByVal Pidl As Long, ByVal pszPath As String) As Long

Public Declare Sub CoTaskMemFree Lib "ole32" (ByVal pvoid As Long)
    
Public Const CSIDL_PERSONAL = &H5
Public Const CSIDL_DESKTOPDIRECTORY = &H10
Public Const MAX_PATH = 260
Public Const NOERROR = 0

'@----------- WRAPPER ---------------@

Public Function SpecFolder(ByVal lngFolder As Long) As String
Dim lngPidlFound As Long
Dim lngFolderFound As Long
Dim lngPidl As Long
Dim strPath As String

strPath = Space(MAX_PATH)
lngPidlFound = SHGetSpecialFolderLocation(0, lngFolder, lngPidl)
If lngPidlFound = NOERROR Then
    lngFolderFound = SHGetPathFromIDList(lngPidl, strPath)
    If lngFolderFound Then
        SpecFolder = Left$(strPath, _
            InStr(1, strPath, vbNullChar) - 1)
    End If
End If
CoTaskMemFree lngPidl
End Function

'@------------------------------------@

To get the 'My Documents' path on any system, use:

Code:
  Dim strMyDocs As String

  strMyDocs = SpecFolder(CSIDL_PERSONAL)

  If Dir(strMyDocs) <> &quot;&quot; Then
    ' do stuff
  End If

Here is another good article that defines the constants for other folder locations:


Good Luck!


VBSlammer
redinvader3walking.gif

Unemployed in Houston, Texas
 
I don't have experience with API, just heard about problems deploying apps and crashes. I just skimmed over the papers you referenced and dumped the code you offered which worked well.

The code didn't read a folder on acc97, so I included an if statement to check for the existence of C:\My documents in case the current user one does not exist. It worked on both acc97 and acc2002, which is my need now. It seemed dirty to me, though!

Well, I thank you guys for the good effort. I'll use the API stuff, VBSlammer! I imagine that handling folders with API is just one among several other subjects it might address. I'll try to locate some API paper which addresses other very more common solutions for Access app.

I couldn't find MacCaillean's System.PrivateProfileString command on both VBA Help of these foreign language Access.

Best regards
Werneck

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top