I would like Access to "automatically" play a song whenever a combo box is updated. I want that song to change depending on the contents of the combo box. Unfortunately the below code returns:
Constant expression required
Here's the code I'm working with:
Private Sub cboCustomerID_AfterUpdate()
txtSongFile = Me.cboCustomerID.Column(2)
Me.Refresh
Const conMEDIA_FILE_TO_OPEN As String = Me.txtSongFile
Me![WindowsMediaPlayer1].openPlayer (conMEDIA_FILE_TO_OPEN)
End Sub
Any help would be greatly appreciated!! Thank you.
The error you mentioned is a compile error, not a run time error. It is caused by this line:
Const conMEDIA_FILE_TO_OPEN As String = Me.txtSongFile
The reason is that the VBA rule for what you can use on the right side of the = sign is quite restrictive. This is what Access' online help says:
"Literal, other constant, or any combination that includes all arithmetic or logical operators except Is."
But you should be able to use a variable instead:
Dim strMEDIA_FILE_TO_OPEN As String
strMEDIA_FILE_TO_OPEN = Me.txtSongFile
Related
I am using MS Access 2016, Windows 10, USAF Laptop running SDC.
I am having an issue with debugging some code. When I hit a line in Break mode I get a Run-time Error '13':Type Mismatch, When I run the exact same code outside of break mode, the code functions properly.
Here is the basic code that is causing me problems:
Private Sub btn_ReqKey_Click() 'This is not all of the code only those lines that are necessary
Dim trgt_CACID as String
Dim Temp_KeyCode as String
trgt_CACID = Me.CACID.Value 'Pulls value from short text field of "1234567890"
'Here is the issue
Temp_KeyCode = UnlockCode(trgt_CACID) 'Run-time Error '13': Type mismacth
More code
End Sub
Public Function UnlockCode(ByVal CACID as String) As String
lots of code that generates a 25 digit Alpha code
End Function
When I click on the button the code fails at "Temp_KeyCode = UnlockCode(trgt_CACID)" Run-tine Error '13': type mismatch.
However, if I reset the code execution and run this in the immediate window without pressing the button.
trgt_CACID = "1234567890"
?UnlockCode(trgt_CACID)
Returns the proper code without throwing an error. So the function UnlockCode() works properly, just not at execution.
Additionally, When in break mode from pressing the button and I attempt the same line of code in the immediate window, it returns the same run-time error '13': Type mismatch.
Furthermore, I exit Break mode, reload the variable and execute the same line of code it works again.
So I know something in the execution environment is being affected, I cannot figure out what that is. This is the first time I have seen a disparity with how the immediate window functions as compare to code execution.
Side Note/Quirk, in the immediate when I enter the function, the helper pops up to guide me for the variables required. When I type the same in the module code, the helper pop ups do not display. So weird. Right? Further evidence something is different between the two environments.
Any help or assistance would be greatly appreciated.
Thanks in advance.
KO
Based on the discussions with Erik A and Gustav, I changed the Function name that was having a conflict with a table field name I was using. Once I changed everything to be dissimilar: table Field Name, Form Textbox name, and Function being called. Everything worked fine. So apparently the context I was calling from already had something using the name of the function and that field name was being picked up and sent to a function with the same name as the field. This created a subtle conflict that explains the disparity between execution in the context of table and form and when being Manually called in the Immediate Window AND why it worked in the Immediate window, but not during execution. Thank you Erik A and Gustav for their assistance.
Try running it with a dummy function:
Public Function UnlockCode(ByVal CACID As Variant) As String
UnlockCode = String(25, "0")
End Function
If that runs, debug your secret function.
I've got a form frm_New_Datasheet to enter datasheets from volunteers into a database I'm making. For the volunteer id text box txt_Vol_ID I've got two buttons to either search for an existing volunteer or add a new one, each opening a respective form. I want the Vol_ID from frm_Volunteers to get passed back to frm_New_Datasheet once I add a new volunteer.
To do this, I added the following code to my volunteer form:
Private Sub Form_AfterInsert()
If Not IsNull(Me.vol_Vol_ID) Then
Dim volVal As Double
volVal = Me.vol_Vol_ID
Forms!frm_New_Datasheet!txt_Vol_ID.Value = volVal
End If
End Sub
And it worked perfectly. Then I tried creating my 'Search for Volunteers' form, added the same code to it, but now I get the following error on both:
Run-time error -2146500594 (800f000e):
Method 'Item' of object 'Forms' failed
The debugger is highlighting the line where I set the .Value = volVal so I've looked for other ways to call that text box but no luck yet. I've tried
Forms([frm_New_Datasheet]).[txt_Vol_ID] = volVal`
but that gives me a different error:
Run-time error '2465':
Microsoft Access can't find the field '|1' referred to in your expression
I'm pretty lost on this because I think I'm calling the other form's text box properly, and when I search for the first run-time error the best advice I've found is "Sometimes VBA is buggy and try restarting" which hasn't helped. So far this is the best idea I've had to pass the value from one form to another. If there's another place I can drop this code or another way to skin this cat I'm all ears.
Thanks!
Looks like you should check form/control names more carefully and make sure that the form frm_New_Datasheet is open.
Here is the correct variant for referring using Forms collection by form name:
Forms("frm_New_Datasheet").txt_Vol_ID = volVal
I'm not sure if this actually counts as an answer but after HansUp helped me through with some trouble shooting in the comments above I just copied the form that wasn't being found (still not sure why), pasted it with a new name, and deleted the original form. After that everything is working well.
I have a small bit of code for a much larger MS Access database that likes to behave erratically. I can't say for sure, but it seems to change when I compile or recompile the database. Here's the code:
Private Sub FindCtls()
Dim DatType as String
Dim thisCTL as Control
DatType = Right(ActiveControl.Name, Len(ActiveControl.Name)-4)
For Each thisCTL In Me.Controls
If InStr(1, thisCTL.Name, DatType, 2) > 0 Then
Select Case Left(ThisCtl.Name,4)
Case "Sel_"
Set TgtCtls(0) = thisCTL
' A buncha other Cases
End Select
End If
Next thisCTL
End Sub
The problem is, when I step through the code, for some reason thisCTL is being passed as the value of the control, not the control itself. I've looked everywhere for this answer, that brought me to using the "Set" command instead of just putting TgtCtls(0) = thisCTL. That worked until I decompiled the DB and recompiled it. Made one change (changed the name of the first variable), compiled it again, and it worked. As I got into work today, someone told me that it was no longer working. Any ideas would be greatly appreciated.
Edit: I forgot to mention that TgtCtls is a form-wide array that was Dim'd at the top as such:
Option Compare Database
Dim TgtCtls(2) as Control
I am trying to have the file dialog box pop up so the user can select a file path to export a file in VBA but for some reason it throws this error on the following line of code.
Error: Method 'FileDialog' of object '_Application' failed
Code: longResult = Application.FileDialog(msoFileDialogFolderPicker).Show
All Code:
If choice = 6 Then
Dim intResult As Long
Dim strPath As String
'the dialog is displayed to the user
longResult = Application.FileDialog(msoFileDialogFolderPicker).Show
'checks if user has cancled the dialog
If intResult <> 0 Then
'dispaly message box
Call MsgBox(Application.FileDialog(msoFileDialogFolderPicker _
).SelectedItems(1), vbInformation, "Selected Folder")
End If
Else
End
End If
I am really unsure how to fix this issue. I checked my syntax and everything.
I know that this is a bit of an old question at this point, but since it doesn't actually have the answer, and I needed one today, I'm going to chime in with what I found just in case anyone else needs the answer too.
To fix this you need to add a reference to "Microsoft Office [yourversion] Object Library" in Visual Basic Editor >> Tools >> References...
The dialog in question should look like this:
Was trying to do the same thing myself and found this question. I realize that it is over a year old.
Try using the actual number (4) instead of msoFileDialogFolderPicker, that worked for me. I think something needs to be installed for the msoFileDialog constants to be initialized, when I tried printing any of the constants defined in the help file in the immediate window nothing was printed.
Also, why does your code have one variable longResult and one variable intResult?
Almost nothing is an integer in VB6 as integer is a VB4 16 bit type. Win32 Integers are called Long in VB6/VBA.
This was to make porting 16 bit code to 32 bit easy.
Check out http://msdn.microsoft.com/en-us/library/office/ff865217%28v=office.15%29.aspx for more information on proper syntax with FileDialogue.Show method. It appears you need a Set in front of your variable.
If you are after some cool UI, you can checkout my Github for sample database using .NET wrapper dll. Which allows you to simply call a function and to open filedialog with file-drag-and-drop function
Dim FilePaths As String
FilePaths = gDll.DLL.ShowDialogForFile("No multiple files allowed", False)
'Will return a JSONArray string.
'Multiple files can be opend by setting AllowMulti:=true
here what it looks like;
I have a Microsoft Access Popup Form which I use to lookup addresses. Once the user has found the postcode, the address is then put into various text boxes on the form it was launched from. The problem is, this popup form is launched from various forms throughout the database and so the text boxes it puts the result into are in different locations.
I tried to work around this in the following way. I have a switchboard which is open at all times so I have a hidden Textbox on there which I programmatically put the name of the form I am launching the popup form from. I then declare a string variable which is set to the current value of this hidden textbox like so:
Dim currentForm As String
currentForm = [Forms]![foo]![bar]
I then tried to put my address details into the relevant textboxes like so:
Forms!currentForm![txtCurrentAdd1] = rst![Line1]
However this isn't working as planned, what am I doing wrong?
Thanks
Either:
Dim currentForm As String
''Not sure where the two parts are coming from
''but you cannot have them like that
currentForm = "foobar"
Forms(currentForm).[txtCurrentAdd1] = rst![Line1]
Or
Dim currentForm As Form
Set currentForm = Forms![foobar]
currentForm![txtCurrentAdd1] = rst![Line1]
You might like to read up on bang vs dot.
Mind you, the whole thing looks a little like you are swimming upstream.
You can access controls on other forms like this:
Dim FormName As String
Dim ControlName As String
FormName = "YourForm"
ControlName = "YourTextbox"
Forms(FormName).Controls(ControlName) = "New Value"
You could also use the OpenArgs property. To open your reusable popup, use:
DoCmd.OpenForm FormName:="frmPopup", OpenArgs:=Me.Name
in order to pass the caller's name.
In the frmPopup, you can refer to Me.OpenArgs to get that information.
I know this is an old post but this might be useful for new readers. The simple way I get round the problem of passing variables between modules and forms is to use the VBA registry area. It's a very simple and easy to use technique and keeps projects nice and clean.
To save a value:
SaveSetting AppName, Section, Key, Setting
To get the value:
GetSetting AppName, Section, Key, [Default]
This avoids loosing track of variables and creating hidden controls on forms. It also works cross platform without change on a PC and a Mac.
I then usually set a load of constants in my projects to define the first three parameters:
Public Const REG_PROD = "My product name"
Public Const REG_CONFIG = "Configuration"
Public Const REG_SETTING1 = "My Setting 1"
So the above calls would become:
To save a value:
SaveSetting REG_PROD, REG_CONFIG, REG_SETTING1, "My Value"
To get the value:
GetSetting REG_PROD, REG_CONFIG, REG_SETTING1, "Value not set"
I used to use the hidden control method but it adds a lot of management and you can't always the variable values whereas you can keep an instance of RegEdit open for debugging purposes.
I think
Me.Controls(variablelabelName).Caption
you can write code to make the variable change and use as the label's name to create the unique caption for each label.