4

In Access VBA, I want to use values from a "Settings" table, instead of hard-coding folder locations etc. in the code. I can't figure out how to load a value from the table and use it in the code.

Dim oFSystem As Object
Dim oFolder As Object
Dim oFile As Object
Dim sFolderPath As String



sFolderPath = "C:\Documents and Settings\Main\Desktop\Files" 'BAD BAD, I WANT TO AVOID THIS

I have created a table "Settings", and I want to use the value

SELECT TOP 1 Settings.SettingsValue FROM Settings WHERE (((Settings.SettingName)="Files Folder Location"));
3
  • 1
    What's with TOP 1 ? How many records have SettingName="Files Folder Location" ? Commented Sep 20, 2010 at 19:30
  • If you have only one record that matches the criteria, Hans's method is better. Commented Sep 21, 2010 at 0:38
  • Heh, ya, I didn't even think of that. I just copy/pasted his query. Commented Sep 21, 2010 at 2:40

2 Answers 2

7

You could use the DLookup function if you have only one record where SettingName="Files Folder Location".

sFolderPath = DLookup("SettingsValue", "Settings", "SettingName=""Files Folder Location""")
Sign up to request clarification or add additional context in comments.

1 Comment

...and if you don't have but one record, then your settings table is designed wrong.
3

One way:

Dim db As DAO.Database
Dim rs1 As DAO.Recordset
Dim sFolderPath As String

Set db = CurrentDb()
Set rs1 = db.OpenRecordset("SELECT TOP 1 SettingsValue FROM Settings WHERE SettingName="Files Folder Location")

If rs1.RecordCount > 0 Then
    rs1.MoveFirst
    sFolderPath = rs1.Fields("SettingsValue")
End If
rs1.Close
set rs1 = Nothing
set db = Nothing

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.