Page 1 of 1
Populate list of local DSN's
Posted: Wed May 16, 2012 9:22 pm
by jfwhite
I need to be able to populate a dropdown list with the names of all local DSN's. This is contained within the registry key:
HKEY_LOCAL_MACHINE/SOFTWARE/ODBC/ODBC.INI/ODBC Data Sources
Now what?

Re: Populate list of local DSN's
Posted: Wed May 16, 2012 9:58 pm
by sturgis
You could try
get the queryregistry "HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBC.INI\ODBC Data Sources"
If the key doesn't exist "the result" will contain "no key"
I don't have any odbc stuff set up so don't have a way to check this here but it should get you close.
EDIT: Changed to use backslashes.
Re: Populate list of local DSN's
Posted: Thu May 17, 2012 5:24 pm
by jfwhite
Thank you - that is good to know and it works to pull the value of a specific key.
But I described the requirement poorly. What I really need is the listing of key names. For instance, when I click on ODBC\ODBC.ini\ODBC Data Sources in the registry tree, the right pane displays the listing of DSN names. In my case the value of each is SQL Server, and I can retrieve that value by using your suggested approach, but I don't really need the key value, just the name. I would want to populate a list with multiple values such as:
DW1
Lawson_Prod
Kronos_Test
So I'm not sure how to touch those values, and now it seems like I would need some kind of loop to bring them all back.
Thanks again - relative LC newbie here, but my project is coming along nicely except for the DSN issue.
Re: Populate list of local DSN's
Posted: Thu May 17, 2012 6:23 pm
by mwieder
Try listregistry("HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBC.INI\ODBC Data Sources")
I'm not at a Windows computer at the moment, but it has a chance of working. The ListRegistry function was introduced as an experimental feature in LC 4.5. It's still listed as "experimental", so there are no guarantees.
Re: Populate list of local DSN's
Posted: Thu May 17, 2012 6:52 pm
by sturgis
If Marks idea doesn't work you could use shell instead.
The following code will grab a whole branch of the registry and put it into the msg box (only tested on win 7) Since I don't have any odbc datasources my example just grabs anything below ODBCINST.INI (rather than ODBC.INI so that I actually get results back)
Once you get the path pointed to the right place you should be able to parse the returned info for what you need.
Code: Select all
put "REG QUERY" && quote & "HKLM\SOFTWARE\ODBC\ODBCINST.INI" & quote && "/s" into tShell
put shell(tShell)
mwieder wrote:Try listregistry("HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBC.INI\ODBC Data Sources")
I'm not at a Windows computer at the moment, but it has a chance of working. The ListRegistry function was introduced as an experimental feature in LC 4.5. It's still listed as "experimental", so there are no guarantees.
Re: Populate list of local DSN's
Posted: Thu May 17, 2012 7:10 pm
by mwieder
!!! Thanks - I didn't know about that option to regedit. I've got several places where I can put that to use.
Re: Populate list of local DSN's
Posted: Thu May 17, 2012 9:31 pm
by jfwhite
I couldn't get Mark's code to return the data I need (although it did work fine for other registry keys), but the shell approach worked fine. Now I have several entries like:
HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBC.INI\OSUMC_BI
Driver REG_SZ C:\Windows\system32\SQLSRV32.dll
Server REG_SZ osumc-boedgeds
Database REG_SZ OSUMC_BI
LastUser REG_SZ jwhite
Trusted_Connection REG_SZ Yes
So it will be good practice for me to figure out how to parse out the DSN name only. Many thanks to you both.
Re: Populate list of local DSN's
Posted: Thu May 17, 2012 11:05 pm
by shaosean
Look at the entries for
lineOffest and
itemDelimiter.