Don't enter in VBA macro when editing - vba

I have a drop down list column in an Excel worksheet to select a category. I have created a macro who replace the selected value by its ID after category selection (Worksheet_Change event).
Users have to fill the worksheet with ID but thank to this function, they just have to select a category in the drop down list and the ID replace the user choice.
It work well but I have a problem: if the user wants to write directly the ID in the cell, I have an error (Invalid value) and the macro is disabled.
How to do the difference in the Worksheet_Change between a change by the drop down list and a change by writing ? Or is there an other solution ?
Thank you !

If I understood correctly and you ARE using Data Validation:
Data Validation allows the user to optionally trigger an Error message if an incorrect value is entered (This error is the default setting). If you want to allow users to input information NOT in the list, turn off the error message by doing the following:
In the Data Validation dialog box, you will notice there are actually 3 tabs: Settings, Input Message, and Error Alert. Select the Error Alert tab, then UNCHECK Show Error Alert after invalid data is entered.
Does this resolve the problem?

Related

Getting a type mismatch but combo box still searches and displays data. ID column is being used to pull through Name but doesn't work

Error popup after click ok on type mismatch
I am using a combo box to search for records and it is telling me I have a type mismatch. I have been unable to find where this mismatch would be. The combo box still performs the desired function of selecting all records based on their "region"
I am using an ID field which is selected using the combo box but I want to pull through a name as well so I have input an unbound textbox and used VBA on change in the ID to update the name to the corresponding ID in the unbound textbox
Form
For privacy reasons I snipped around some of the other fields showing data.
The VBA in the on_change event in ID goes
Private Sub txtSupplierID_Change()
Me.txtSupplerName.Value = Table("tblSuppliers").SupplierName
Where Table("tblSuppliers").ID = txtSupplierID
End Sub
I'm aware this code could be very wrong but I could not find anywhere else that showed me how to do what I was trying to do. Any suggestions on how to actually do it would be appreciated. If I have not been as detailed as needed or yo have any questions please ask.
Edit for Clarification:
The form is not being used to save data. It is only being used to display data and issue an output to a report.
Code is not just wrong, it is nonsense. Cannot reference a table object like that. And that Where usage is not valid. One way to pull value from a table is with DLookup domain aggregate function. Advise use AfterUpdate instead of Change event.
Private Sub txtSupplierID_AfterUpdate()
Me.txtSupplerName.Value = DLookup("SupplierName", "tblSuppliers", "ID = " & txtSupplierID)
End Sub
Might want to correct textbox name from txtSupplerName to txtSupplierName.
However, should not save both supplier ID and supplier name into this table. There really should be no need for this VBA. Just put DLookup() expression in a textbox ControlSource. For other methods of pulling the supplier name to form, review answer in MS Access Identical Comboboxes for Autofill

MSAccess VBA: Pass values from a subform to a seperate form

I have a form frmDetail that contains several locked fields I want populated only when another form has been filled out, in the name of having some kind of standardization in my data. This other form frmSignOut is used to enter in a date and location that will populate the fields on frmDetail. frmSignOut also contains a subform subULookup that looks up users from a different table using an identifier number. The resulting last name, first name and phone # should also be passed to frmDetail. I also hope to combine first and last name somehow into a Last,First format.
My approach so far has been to open frmSignOut modally with acDialog and I inserted Visible=False into the On_Click event on frmSignOut. Then I try to reference my subform fields and set them to the desired fields on frmDetail. I finish by refreshing and then closing the dialog form.
Private Sub CmdSignOut_Click()
DoCmd.OpenForm("frmSignOut"),,,,,acDialog
If CurrentProject.AllForms("frmSignOut").isLoaded=True Then
Set Forms!frmSignOut!SubULookup.PhoneNbrTxt=Me.ContactNbrTxt
DoCmd.Close (acForm), ("frmSignOut")
Me.Refresh
End If
End Sub
I have only been able to get this far, trying to pull the first field PhoneNbrTxt. I can get frmSignOut to open, I put in data and when I click my 'close' command button I get run-time error 438: Object doesn't support this property or method.
The line highlighted is where I try to reference my subform field. I also have a 'Cancel' button added to frmSignOut that just closes the form, I have no errors there.
Again I'm completely new to Access without much prior experience in anything related, I'd appreciate any knowledge you guys can throw at me.
EDIT: I've been able to successfully pull the value to Me.ContactNbrTxt by adjusting my code to the below.
Me.ContactNbrTxt = Forms!FrmSignOut!SubULookup.Form!PhoneNbrTxt
It looks like the missing part was the Form! right before the control name, along with formatting this correctly and dropping Set.
Im still trying to work out how to combine first and last name before also pulling those to frmDetail, if anyone can help there.

How to reference tables/queries/reports in access using an alias name?

In my Access database, I have tables with the naming convention of "t_customers" or "t_orders". I have a combo box which lets me pick from these options. For a better look, I want to have the options in the combo box without the "t_", so one of the options is just "customers". Then in VBA, I can access the combo box's value of "customers" and then pass that as a parameter to my function which will export the contents of the associated table "t_customers" to an excel file.
Is this possible to do? I can think of using a select case statement and for each combo box value I manually assign the related table value, but there has to be a better way. Is there some sort of short cut that does this in Access using a relationship or similar feature without coding? Or is there a simpler way to code this?
EDIT:
I am also trying to get the value of the current item selected in my combo box named "cbTable". I have no macros attached to the combo box. I have a button that when pressed it runs a macro which calls my VBA function. In the function I have this code:
Dim cbValue As String
cbValue = cbTable.Value
MsgBox (cbValue)
I get this error:
Runtime Error '424'
Object Required
The error is on the
cbValue = cbTable.Value
line. What is wrong with this code and how do I get the current value of the combo box?
Thanks in advance.
You can set your RowSource for your combobox to
SELECT MSysObjects.Name, Replace(MSysObjects.Name,"t_","") AS Expr1
FROM MSysObjects
WHERE (((MSysObjects.Type)=1) AND ((MSysObjects.Flags)=0));
Then set the Column Count to 2, the Column Widths to 0;2 (the second number just needs to be any positive number) an the Bound Column to 1.
Now when you go to call your function the value of the combobox will be the real name of the table, t-customers, but what you saw on your form was customers.
As for accessing that combobox a few things need to be in place.
Let's say you have a form, Form1, and your combobox, cbTable, is on that form.
So when your form runs it looks like this
Now let's say you have an OnChange even for that combobox
Notice that when you type cbTable it appears in the intellisense (ctrl+space). This tells you that your object is accessible at this scope.
Now if you were in another module where that variable is out of scope you would get this error when you try to compile.
Because Module1 has no idea what cbTable is. However, you can reference anything so long as it's fully qualified. So if we run this test with your form open, and "orders" selected all 3 of these lines are equivalent
Forms("Form1").cbTable
Form_Form1!cbTable
Forms("Form1").Controls("cbTable")
Notice at the top of all my modules it says Option Explicit. This forces your code to basically be checked for syntax/scope validity before you run it. Tools > Options

Excel validation list allowing invalid inputs

I have a worksheet with several different validation lists using named ranges on a different sheet. I realized that the cells using the ranges with blanks in them still allow the user to type in whatever they want. If there's no blank in that defined range, then the normal Excel error message pops up. Anyone know why this happens?
Ex:
Both types of validation lists are the default settings with "Ignore blank", "In-cell dropdown", and the Error Alert checked.
Some of my lists have spaces in between them but I would still like to restrict users to selecting only the choices provided by the dropdown.
Update:
I tried unchecking the "Ignore Blanks", but one minor issue this causes is that now the user can't press "Backspace" to clear the cell. They have to actually select one of the blank rows in the drop down list.
I discovered another issue which may only be specific to my code (not the example):
-if the cell already has an option selected
-the user tries to delete the cell via "Backspace" and the Excel error message pops up
-if I click Retry and click on the drop down list again, I get 3 of these:
-the error is from the Worksheet_Change sub and occurs despite me having an error handler within it
You need to uncheck the option for Ignore blank in the data validation dialog box.
Then Excel will give an error message when the user tries to enter data even if there are empty cells in the named range used by the data validation.

How to change the Contents of a msg box

I have a form for creating a record, I also have a button on the top of the form for to return to a form called home. When the form is being viewed and half the information is put in and the home button is clicked an error pops up saying " you cannot add or change a record beacasue a related record is required in a table "Elements"." How do I Change what the content of the error message is ?
You can put checks in your button_click sub to circumvent -- check to make sure all fields are filled in, and if not, display your own message box followed by
Exit Sub
That will short circuit the rest of the method so you should not get the standard message.
Here is a resource on error-handling in VBA: http://allenbrowne.com/ser-23a.html
I'm not sure, however, if you can create a handler in VBA for standard program error messages.