Set Variable Based Off User Selection (1 Viewer)

GC2010

Registered User.
Local time
Yesterday, 17:58
Joined
Jun 3, 2019
Messages
120
Is my best plan of attack here using
Code:
If .... then....else....end if

I have a combobox with 40 possible options in it. Based off the selection in the combo box I need to open a file browser to a specific folder. What seems most logical to me is to do
Code:
'Declared Public in diff module
Public FullAddress As String

Private Function OpenFolder(name As String)
Dim staticFolder, empolder As String

staticFolder = "C:\Personel\Employees\"

If name = "James" Then
    empolder = "J23815"
    FullAddress = staticFolder & empolder & "\" & Year(Now) & "\"
End If
if name = "Rachael" Then
    empolder = RC9291"
    FullAddress = staticFolder & empolder & "\" & Year(Now) & "\"
End If
End Function

Currently the empolder is not stored in a database anywhere it is a alpha-numeric combo that a supervisor created.
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 17:58
Joined
Oct 29, 2018
Messages
21,469
Hi. What is the Row Source of the combobox? It would be easier if you could include the folder name as a separate column in it. Or, is there a "rule" you can use to calculate it from the selection made?
 

jdraw

Super Moderator
Staff member
Local time
Yesterday, 20:58
Joined
Jan 23, 2006
Messages
15,379
Do you have a list the relates empolder and Name? If so, post a copy.

This does NOT do what you think.
Dim staticFolder, empolder As String
staticFolder will be dimmed as a variant
empolder will be a string datatype.

With Access vba, you must explicitly Dim variables,

1)Dim staticFolder as string , empolder As String OR

2)Dim staticFolder as string
Dim empolder As String

Name is a reserved word in Access.
 

isladogs

MVP / VIP
Local time
Today, 01:58
Joined
Jan 14, 2017
Messages
18,218
If the folder is fixed for each selection I would store that info in a table.
Your combo row source could then obtain both username and folder name as previously suggested by DBG.
Doing that means no need for If..Else..End If code. Plus its easier to edit in the future if names of folders are added/edited
 

GC2010

Registered User.
Local time
Yesterday, 17:58
Joined
Jun 3, 2019
Messages
120
Hi. What is the Row Source of the combobox? It would be easier if you could include the folder name as a separate column in it. Or, is there a "rule" you can use to calculate it from the selection made?

The row source is one field from a linked SharePoint table

I could create a new local access table that houses the full path and the employee name and maybe use Dlookup in the bbq to get the full folder path
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 17:58
Joined
Oct 29, 2018
Messages
21,469
The row source is one field from a linked SharePoint table

I could create a new local access table that houses the full path and the employee name and maybe use Dlookup in the bbq to get the full folder path
And the SharePoint List does not have the folder information?
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 17:58
Joined
Oct 29, 2018
Messages
21,469
Unfortunately not, I do not have ability to add columns/fields to the SharePoint site.
Then, I guess you'll have to create a local table for it then.
 

Micron

AWF VIP
Local time
Yesterday, 20:58
Joined
Oct 20, 2018
Messages
3,478
sounds like combo row source is a value list. While you can create a multi column combo this way, the FullAddress, staticFolder and empolder values would probably be better of coming from table(s) to the combo via query. The more you can get from it, the less code you'll need.

Alternatively, a Select Case block is often better than a whole bunch of IF's.

Forgot to mention, combo AfterUpdate may be a better event to use.
 
Last edited:

GC2010

Registered User.
Local time
Yesterday, 17:58
Joined
Jun 3, 2019
Messages
120
sounds like combo row source is a value list. While you can create a multi column combo this way, the FullAddress, staticFolder and empolder values would probably be better of coming from table(s) to the combo via query. The more you can get from it, the less code you'll need.

Alternatively, a Select Case block is often better than a whole bunch of IF's.

Forgot to mention, combo AfterUpdate may be a better event to use.

How would you advise using the AfterUpdate of combo?

I am so glad the crew here recommended a local table. I think that is going to be the bets bet since I am at roughly 39 now and the list can grow at any time.
 

Micron

AWF VIP
Local time
Yesterday, 20:58
Joined
Oct 20, 2018
Messages
3,478
by that I mean that since a combo is involved, once a value is chosen the after update event can get the chosen value from the bound column thus there's no need to test for what option was picked. The path at that point is to act accordingly. This is where the Select Case block can shine, or if the path is simpler (such as passing the chosen value to a query) it's just a matter of passing the value of the combo's bound column.
 

isladogs

MVP / VIP
Local time
Today, 01:58
Joined
Jan 14, 2017
Messages
18,218
If the combo has user name as the bound column and folder name as the 2nd column, the after update event can just pull that info using Me.Comboname.Column(1) as the numbering starts at zero.

As I said previously, you can then scrap If...Else..End If. You don't need Select Case either
 

GC2010

Registered User.
Local time
Yesterday, 17:58
Joined
Jun 3, 2019
Messages
120
If the combo has user name as the bound column and folder name as the 2nd column, the after update event can just pull that info using Me.Comboname.Column(1) as the numbering starts at zero.

As I said previously, you can then scrap If...Else..End If. You don't need Select Case either

That is my issue the combobox is bound to a SharePoint table that does not have this information, and I am unable to edit the table to add it in.

I am thinking my best plan of attack is to create a separate local table in the access database with this information, and as you suggest on the after-update event use a VBA VLookup and pull in the location based off the combo-box (SharePoint Table) selected value

Something like this (not actual via syntax)
Code:
=DLookup("FullAddress", "localtable", "name = 'Comboboxvalue'")
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 17:58
Joined
Oct 29, 2018
Messages
21,469
That is my issue the combobox is bound to a SharePoint table that does not have this information, and I am unable to edit the table to add it in.

I am thinking my best plan of attack is to create a separate local table in the access database with this information, and as you suggest on the after-update event use a VBA VLookup and pull in the location based off the combo-box (SharePoint Table) selected value

Something like this (not actual via syntax)
Code:
=DLookup("FullAddress", "localtable", "name = 'Comboboxvalue'")
Hi. If you create a local table, then there's really no need to use DLookup(). You should be able to JOIN your table with the SP List and pull all the data you need into the Combobox.
 

GC2010

Registered User.
Local time
Yesterday, 17:58
Joined
Jun 3, 2019
Messages
120
Hi. If you create a local table, then there's really no need to use DLookup(). You should be able to JOIN your table with the SP List and pull all the data you need into the Combobox.


Hi - thank you for the continued support!!

Okay, so I have created the SQL for my join - which I believe should be set as the Row Source for the combo box. Now, I have only used a combobox for one field, which leads me to my question of how do I capture the EmployeeFolder based off the [Employee Name] selection from my combobox when this is the Row Source?

Code:
Select [_Data].[Emp Name], [_LocalFolders].[EmployeeFolder]
FROM [_Data] Inner Join [_LocalFolders]
On [_Data].[Emp Name] = [_LocalFolders].[Emp Name]
Order By [_Data].[Emp Name];
 

Micron

AWF VIP
Local time
Yesterday, 20:58
Joined
Oct 20, 2018
Messages
3,478
or reverse the order of the 2 fields in the sql and just refer to the combo and forget the column reference. Likely 2 column combo would have column 0 hidden so that you only see name, but the folder value is what the combo holds when you refer to the combo.
 

Users who are viewing this thread

Top Bottom