Is there an easy VBA code using Split() Function to get a list of values for a combo box that was originally from a text box as a string? - vba

On my access form, I have a text box that will be a string of characters with multiple "/"s throughout the string. I want to use the split function to separate this string into a list of values to use for my combo box on a subform.
I know it's somewhere along the lines of:
Public Function MakeList()
Dim MyList as String
Dim txt as String
txt = [myTextBoxField].Value
MyList = Split(txt,"/")
Either:
[myComboBox].Value = MyList
Or:
[myTextBoxField].Value = MyList
End Sub
I am not sure if this is supposed to be on "Form Load" or in a module for the Public Function.
All other code shows a For Loop or Debug.Print. I am looking to store this list as a field in my table and then use that field for my Row Source in my combo box.

First, combobox RowSourceType property must be set to ValueList. Next, VBA sets RowSource property, not Value. List is not a property of combobox in Access. Simply:
Me.myComboBox.RowSource = Replace(Me.myTextBoxField, "/", ";")
Form Load event should be appropriate.

Related

Append Strings and values together to target a form control in VBA

I'm so close to getting this code working, I just need a little push please. I would like to
take the name of a combo box and then add a string to the end, But then get the value of a textbox with that string. This is to create a dynamic function instead of pasting the same code over and over.
Here's what I have so far, after you select something in the dropdown, the data is then pulled to populate the boxes next to it. I have about 8 drop downs so far so that's why I need this to work.
'Combobox after update
Call GrabData(Me, Me.ActiveControl)
Then
Private Sub GrabData(ctl As Control)
'name of ctl/combobox is "Kitchen"
data1 = (ctl.Name & "Size") '"KitchenSize"
'Here is where it all goes wrong
data1.Value = size.value
'size.value is just a textbox for example
End Sub
I can debug this with:
msgbox(data1)
'outputs "KitchenSize"
But I cannot get the value of kitchensize's textbox with data1.value
Error:
Object Required
I have also added Dim As String / Dim As Control.
I will be assigning the variable to some other stuff in this 50 line code I wrote so please don't take the above example as exactly what I intend to do, I just need help appending the ctl.name to a string, then use that to reference another control and so on.
EDIT
For anyone who wants to know, I figured it out.
Dim Ctrl As Control
Dim CtrlName As String
CtrlName = ctl.Name & "Size"
Set Ctrl = Me.Controls(CtrlName)
Ctrl.Value = 'Wherever you want to send the values to
See the edit.
You need to dim it as a string, then use Set Ctrl

How to make a textbox receive changes from comboboxes and another textbox?

Essentially I am taking the inputs from these ComboBoxes shown below:
Along with the changes from this textbox:
And place both of these changes towards another, sole textbox. For instance, if I have my file name format adjusted the following way from the first screenshot, and I type in my client/company name as "apples", the textbox for "File Name" should output to this:
I want the user to type in their preferred "client name" and make my program add those changes automatically, without compromising the values/inputs from the ComboBoxes and "Client Name". I tried to look online for something like this, but the solutions provided made very little sense, or were just too confusing for me to understand. Any help will be greatly appreciated!
This is fairly broad; there are many ways it could be solved but I think I'd make it fairly simple:
I'd have the comboboxes in a list in left to right order:
Dim combos = { combobox1, combobox2, combobox3, combobox4, combobox5, combobox6 }
I'd have the replacements in a dictionary in any order, so this Dictionary is essentially a list of KeyValuePairs, the Key is what we find, and the Value is what we replace it with:
Dim repl = New Dictionary(Of String, String) From _
{ _
{"Client Name", _companyClientName.Text}, _
{"Month", DateTime.Now.ToString("MMM")}, _
{"Year", DateTime.Now.ToString("yyyy")}, _
{"Please Select", ""} _
}
And perform a set of replacements in a loop:
filenameTextBox.Clear()
For Each c as ComboBox in combos
'to track if we perform any replacement
Dim changed = False
'for each r in the list of replacements
For Each r as KeyValuePair(Of String, String) in repl
'if the text in the combo is something we replace
If c.Text = r.Key Then
'append a replacement-performed version
fileNameTextBox.AppendText(c.Text.Replace(r.Key, r.Value))
changed = True 'track that we made a change
Exit For 'don't make any more replacements
End If
End For
'if we didn't change anything, just put the text of the combo in literally
If Not changed Then fileNameTextBox.AppendText(c.Text)
End For
All this code would go in a method and then event handlers for "combo selected item changed" and/or "company name text box text chnaged" would call the method

How to refer to Word Field by name in VBA Code?

I want to update a word field content using VBA. I already have a code that works, but I have to refer to the field's index instead of field's name, which I would prefer.
The code I have is as follows:
Sub UpdateField()
Dim myString As String
myString = "asdf"
ActiveDocument.Fields(1).Result.Text = myString
End Sub
Suppose that the field's name is MyField1. The following code will not work (I get the Run-time error '13': Type mismatch'.
Sub UpdateField()
Dim myString As String
myString = "asdf"
ActiveDocument.Fields("MyField1").Result.Text = myString
End Sub
I create my word fields from File Menu > Informations > Advanced Properties > Custom Tab.
So, how to refer to the field's name when we want to update its content?
These are DocProperty fields. If you press Alt+F9 you'll see the field codes.
A DocProperty field references (links to) a Document Property. The Document Property has the name, but this does not "name" the field. Nor is it possible to update a DocProperty field directly since it links to the Document Property. It might be possible to make it temporarily display something else, but this will be lost any time the field is updated.
In order to update a DocProperty field it's necessary to update the underlying Document Property. For example
Sub EditDocPropUpdateDocPropertyField()
Dim doc As Word.Document
Dim prop As Office.DocumentProperty
Dim propName As String
Dim newPropValue As String
Set doc = ActiveDocument
propName = "MyField"
newPropValue = "new value"
Set prop = doc.CustomDocumentProperties(propName)
prop.value = newPropValue
doc.Fields.Update
End Sub
In the VBE, the Object Browser is a great way to find out what's possible. When I find Word>Field and click on it, I see a list of the members of Field. Name is not in that list. This means that the field object does not have a Name property. That's why you get the error.
You can work around this. One way is to create a bookmark around the field in question. Then in code, find the bookmark by name, then find the field by index inside the bookmark range.
Sample to set text to fields by field name:
ThisDocument.FormFields.Item("MyField1").Result = "hello"

Combobox assignment of variable

If I put:
variableName = namecombobox.selectedItem
or
Dim variablename as type = namecombobox.SelectedIndex
Visual Studio gives me the error
Option Strict disallows conversions from object to string.
I can fix this by putting:
variableName = convert.ToString(namecombobox.SelectedItem)
Are all values contained in a combobox automatically treated as a non-string even when they are string values (in this case "Male" & "Female") and what is the correct way of assigning the value selected in a combobox to a variable?
This is normal, the ComboBox.Items property is a collection of System.Object. You should use the item's ToString() method, just like ComboBox does to generate the visible text.
Dim variableName As String = namecombobox.SelectedItem.ToString()
Or use CStr(), the VB.NET way.
If you are using this, assuming you select 'Selection1' on the combo box:
Dim x As Boolean
Dim MyVariable As String = ""
MyVariable = ComboBox1.SelectedItem.ToString()
If MyVariable = "Selection1" Then
x = True
Else
x = False
Pretend the above code is YOUR code... This is CORRECT for selecting strings from a ComboBox. Insert a breakpoint on the IF statement checking "MyVariable"- you will see the variable content if you hover your mouse over the variable name. It is a quick way to view the contents of your variable. If hovering above the variable shows an empty string ("") or simply Nothing, then it hasn't picked up any selected item.
In my code above, if I clicked an item containing the words "Selection1", the 'MyVariable' would contain a String of "Selection1" and the boolean variable 'x' would also read as TRUE.
If you're getting reading errors by comparing the variable you have issues elsewhere in your code.

How to change the title bar text of a Userform in VBA?

I'm maintaining an old-ish application written in VBA for Excel 2002 (XP)/2003, and am trying to internationalise it.
To do this, I read in the translated strings dynamically and update the various controls on my userform by updating their .Caption property.
This works as expected for all controls but not for the form itself -- when I change the form's .Caption property, then the title bar keeps displaying the "hard-coded" value, and the new value is instead displayed just below it, at the top of the "canvas" of the form itself.
Is it possible to change the title bar text of a UserForm after it has been shown, or do I have to change the .Caption property of the form before it is shown in order for it to be reflected in the title bar rather than in the canvas/client area?
My code looks something like this:
' in frmFoo
Private Sub UserForm_Activate()
' ...
TranslateDialog Me, "frmFoo"
' ...
End Sub
' in a VBA module
Sub TranslateDialog(pForm As UserForm, pFormName As String)
Dim new Caption As String
Const notFound As String = "###!!##NOTFOUND##!!###"
' ...
' GetMessage() returns the translated message for a given key, or the
' default value (second parameter) if no translation is available.
' The translation key for the form caption is the form name itself.
newCaption = GetMessage(pFormName, notFound)
If newCaption <> notFound Then pForm.Caption = newCaption
' ...
End Sub
As I said, the assignment to pForm.Caption does have an effect - but it doesn't write to the title bar of the window, but rather directly beneath it. I'm running Excel 2003 on Windows XP SP 3.
Your frmFoo is not actually the same type as the base UserForm, rather it's internally "descended" from it in VBA's wierd OO implementation so you can't use that reliably as a parameter type, using Object instead will work;
Sub TranslateDialog(pForm As Object, pFormName As String)