invalid outside procedure Error In Access?? Looked at other questions on this site but still do not understand - vba

My code in vba for access was working fine, I had procedures that I wrote for different events that all worked. Then when I went to give my combo boxes and text boxes an assigned row source and now nothing works. I get this invalid outside procedure error. So after hours of trouble shooting I ended up deleting the entire form and then rebuilding it and I still get the same error. So now I just working with one event just until I can get things figured out.
Public Sub cb_op1_AfterUpdate()
tb_LbrRate1.Value = DLookup("LaborRate", "tbloperationsType", "[operationsID] = cb_op1.value")
End Sub
This use to work but now it won't?? tb_LbrRate1.value is a text box and cb_op1.value is a combo box.
I have tried this too
Public Sub cb_op1_AfterUpdate()
End Sub
I left everything blank and I still get the error when I change a value in my combo box?? I just don't get it!!!! any help would be appreciated.

Can you do this:
and check what you get?
This error in MS Access is usually because of code flying not between Sub and End Sub.

Related

"object required" error message when updating labels in a form - Access 2016

I would like to update 2 labels in a form when my code (vba) runs in an Access (2016) database. I kick off the code and I get an "object required" error message when it hits the code below. These labels appear on the "MAIN" form; which is opened when the database is opened.
I tried adding "MAIN." before each line.
I tried adding "Me." before each line.
I would also like to kick off the the sub "Get_Counts" that contains the code below when the form is opened. The is a Private Sub Form_Load" that kicks off when the database is opened. I would like to include in the "Form_Load" sub but haven't done anything like this before.
Nothing is working for me. Any suggestions would be greatly appreciated. Thanks for the help....
Sub Get_Counts ()
....do stuff....
ProgBar1.Caption = "Retrieving Counts for: "
ProgBar2.Caption = "Center 503"
....do more stuff....
End sub

VLookup in VBA userforms - string lookup value

I have got an issue with a vlookup function in VBA userform. Using below code it only seems to work when it is numeric value, however I have got values like G9000 which when selected throw error back I have tried to cast it as string but mismatch error comes up. There is multiple threads on the subject online, however it doesn't seem to work in my case. I am using this to update value in one of the boxes using change event. What am I doing wrong?
Private Sub cb_safety_observers_id_Change()
With Me
.tb_safety_observersName = Application.VLookup(Val(Me.cb_safety_observers_id), employeeSheet.Range("emp_list"), 2, 0)
End With
End Sub
.

Finding a syntax error in a large VBA/MS Access project

I have a MS Access database that displays an error message about a SQL syntax error on launch. "Syntax Error in query. Incomplete query clause." It also shows another error a few seconds after I hit "OK" on the first one.
Here's the two errors: https://imgur.com/a/PesjIFk
But it doesn't tell me where the syntax error is. There are SQL statements in a bunch of different places all over this project. This is a really large project and it wouldn't be practical to just look through all the code hoping that I notice an error someplace. How can I find out where this error is?
EDIT: Ok, so apparently you have to have a keyboard that has a "Break" key on it in order to even find where the error is. Wow. Fortunately I happen to have one. Here's the code that Access takes me to if I press break when I see the error message. This code is for a subform of another form. It highlights the first line (Private Sub Form_Current()).
Private Sub Form_Current()
If NumEnums > 0 Then
CurrentEnum = val(Nz(bit_value_edit.value)) 'Update CurrentEnum to the currently selected enum
Call UpdateEnumsLabel(Me.Parent![enums label]) 'Update label
End If
End Sub
...and here's UpdateEnumsLabel():
Public Sub UpdateEnumsLabel(ByRef label As Control)
If NumEnums > 0 Then
label.Caption = "Enums: " & CurrentEnum & "/" & NumEnums
Else
label.Caption = "Enums: 0"
End If
End Sub
The definition for CurrentEnum:
Public CurrentEnum, CurrentPort, CurrentFile, CurrentGroup As Long
I'm thinking that this error is unrelated to the code in Form_Current(), but Access is highlighting that line because the error happens when the form is opened. But the form doesn't contain anything that uses a query, so I'm confused as to what query Access has a problem with.
When the error Message pops up, Use Control+Break. It will take you to the line causes the issue.
You should also open a module and form the debug option in the VBA editor select "Compile All Modules"
And since it appears to happening on open/load, you can check both the macros and the main modules to find anything that triggers on AutoExec.
Often ctrl-break will hit the line where you errored out. However, in the case of multiple “events”, and code without error handling, then often the error occurs in the routine that called the code, not the actual code that caused the error.
What I would do launch the application (hold down the shift key to prevent any start up code, or forms running).
Now that you launched the application, but without forms or code running, then check for an autoexecc macro (if there is one, check what code it attempts to run).
If there not an autoexec macro in use, then check under file->options->current database. In this view, you can look/see/determine what the name of the start-up form is.
Once you determined the start-up form (or start up module/code) called from autoexec macro, then you can simply add a blank code module, and place in your code the SAME command that is used to start your application.
So let’s assume no autoexec macro, and you determine that the start-up form is frmMain.
So now, we can launch the application (hold down shift key to prevent any start up code from running). Now, type into a new “test” standard code module the following code:
Sub MyGo
Stop
Docmd.OpenForm "frmMain"
End sub
So you type in above code, hit ctrl-s to save the above code. Now with your cursor anyplace inside of the above code, you simply hit F5 to run.
At this point you hit/stop on the “stop” command in the debugger. At this point, you then can hit f8 to step to the next line of code. If the form in question calls other code etc., you still be able to single step, and “eventually” you hit the line which causes the error.
While the application may be large, a simple look at the start up forms (and huts then the start-up code) means that you ONLY really need to trace and look at the start up code – not the whole application.

microsoft access 2010: Private Sub Form_Load() not running, .setFocus not acknowleged.

So I am trying to create a login form for a database application and I used this article as my guide.
http://accesshosting.com/create-login-form-ms-access
I followed the steps to the best of my knowledge. However, in creating the vba code for the on click procedure for the "Ok" button for the login form. using the code in the webpage I receive an error on the Private Sub Load_Form part of the codeblock. Other users seemed to have issues with this part as well.
The code in which the error is thrown is:
Private Sub Form_Load()
Me.txtUserName.SetFocus
End Sub
.SetFocus is highlighted in this case and error message is shown below.
method or data member not found
I tried researching this question and I saw that there might be an issue with naming of the controls. However, the names all matched up. When i typed Me.txtUsername it popped up with intellisense. However the .setFocus didn't appear as I was typing it.
Not sure what's up. The datatypes for each field are as follows.
userName: text, userLogin: text, userID:number, password: text
Short Version: the form cant load and its because .setFocus isn't accepted as a method. Why?

"Sub or Function not defined" when trying to run a VBA script in Outlook

As a first step in creating a VBA script to resize a currently selected image to 100% x 100%, I'm trying to reproduce the example in http://msdn.microsoft.com/en-us/library/ee814736(v=office.14).aspx. The macro is very simple:
Sub Test()
MsgBox ("Hello world")
End Sub
The VBA script was simply created in "Project1" which opens by default when one presses Alt+F11. However, I keep getting the error "Sub or Function not defined" when trying to run the VBA script (Figures 1 and 2).
How can I make the VBA script 'accessible' to Outlook?
Figure 1 Running the "Test" macro in Microsoft Outlook
Figure 2 "Sub or Function not defined" error, with module tree in the background
I solved the problem by following the instructions on msdn.microsoft.com more closely. There, it is stated that one must create the new macro by selecting Developer -> Macros, typing a new macro name, and clicking "Create". Creating the macro in this way, I was able to run it (see message box below).
I had a similar situation with this issue. In this case it would have looked like this
Sub Test()
MsqBox ("Hello world")
End Sub
The problem was, that I had a lot more code there and couldn't recognize, that there was a misspelling in "MsqBox" (q instead of g) and therefore I had an error, it was really misleading, but since you can get on this error like that, maybe someone else will notice that it was cause by a misspelling like this...
This error “Sub or Function not defined”, will come every time when there is some compile error in script, so please check syntax again of your script.
I guess that is why when you used msqbox instead of msgbox it throws the error.
This probably does not answer your question, but I had the same question and it answered mine.
I changed Private Function to Public Function and it worked.
I need to add that, if the Module name and the sub name is the same you have such issue.
Consider change the Module name to mod_Test instead of "Test" which is the same as the sub.
I think you need to update your libraries so that your VBA code works, your using ms outlook