MS access function vba, macro [closed] - vba

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I'm having a small issue while making a report in MS Access. I wrote a function in vba, and created a macro to RunCode (the function). So when I execute the Macro, it runs the function and gives me a message box. See Image..
I've researched this and tried SetWarning OFF but it keeps popping up. How do i get rid of this?

For future reference, a typical "complete" error handling structure looks e.g. like this:
Sub MySub()
On Error GoTo MySub_Err
' Stuff
MySub_Exit:
On Error Resume Next
' ... Stuff that always needs to run on exit can go here ...
' !! This is the important part that prevents the function
' !! from always running into the error handler:
Exit Sub
MySub_Err:
MsgBox Err.Description, vbExclamation, "Runtime Error " & Err.Number & " in MySub"
Resume MySub_Exit
End Sub
MZ-Tools can create this structure automatically (but customizable).

error_handler:
Error_handler:
msgbox err.number & " - " & err.description
this was giving me an error.
I did not do my error_handler: properly (error_handler:). After my function was completed, it would run into error_handler:msgbox err.number & " - " err.description and give me the message box - saying "0 - " because there was no error. I commented it out and everything seems to work okay. Thanks to #HansUp and everyone else for their help.

Related

Need advice with Error Handling for Invalid Path

I could use some advice on how to handle a strange issue that happens to my users.
Issue:
All of my users are on laptops that use VPN to connect to our network. When they disconnect from the VPN and reconnect the network drive also disconnects till they open the drive or folder through file explorer. When this happens and they don't re-establish the connection to the network drive they end up with 3304 error.
Error Handling?
What I'd like to do is setup some sort of error handler to tell them thats what happened with a potential link they could click on to re establish the connection. OR even better just have the VBA code identify that error 3304 has occurred and re-establish the connection automatically for them and no pop up error happen at all.
Has anyone done this? I've done some research but nothing I've found quite fits the criteria for this issue. Looking for any advice or push in the right direction.
I was thinking of something like this where either they get a pop up with a link or skip the pop up all together and find a way to re-connect the drive in the background.
On Error GoTo ErrHandler
'Error Handler
ErrHandler:
If Err.Number = 3304 Then
MsgBox "Error Number:" & Err.Number & vbCrLf & _
"Error Description: " & Err.Description & vbCrLf & _
"This error is due to your VPN being disconnected and reconnected"
Else
End If
We faced that issue a couple months ago. We took the route of actually just showing an informative MsgBox to tell the users what to do to solve by themselves calling a function into the 'Workbook_Open' event that fails if a Central Code for the Company its not found with the Dir() function:
Function IsVPNOn(ByVal strCentralCodePath As String) As Boolean
Dim retBoolean As Boolean
On Error Resume Next
If IsError(Dir(strCentralCodePath & g_strCentralCodeName)) Then
MsgBox "The VPN seems to be disconnected." & vbCr & vbCr & _
"Please check VPN connection if Company toolbar is needed.", _
vbCritical + vbOKOnly, "Company Ribbon"
retBoolean = False
Else
retBoolean = True
End If
IsVPNOn = retBoolean
End Function
Not gonna lie, the second option with skipping and reconnecting without the user being even aware of the problem seems more classy, but it is way trickier as you have to deal with connections inside your code.

A first chance exception of type 'System.Data.DBConcurrencyException' occurred

I'm a beginner in learning VB.Net with Visual Studio 2008. I have Access (mdb) file integrated to my project.
Currently, I need to update the existing record which call up by using table adapter on DataGridView.
On Error GoTo SaveErr
Me.AdmissionTblBindingSource.EndEdit()
Me.AdmissionTblTableAdapter.Update(MasterTblDataSet.AdmissionTbl)
MessageBox.Show(txtApplicantFirstName.Text & " " & txtApplicantLastName.Text & " is " & cboInterviewResult.Text & " for the interview")
SaveErr:
Exit Sub
Refresh()
When I try to run the form, it showed this error.
A first chance exception of type System.Data.DBConcurrencyException occurred
Looking for help.
I solved the problem by change the 'On Error GoTo SaveErr' to 'On Error GoTo Resume' and the problem solved. Thanks

How do I tell VBA code to try again on a specific error?

I have some code which occasionally errors (if someone makes a change in the DB whilst I am trying to read as our locking is set to table level rather than row level and I can't change this).
When it errors I hit debug and then hit continue and it continues on its merry way.
Is it possible on this error only to replicate my actions?
On Error Resume next
will skip over the erroring command and continue on, I don't want this, I want to continue with the command that gave the error as it usually works. However if the error is persistent then there could be a wider issue and we should stop.
I am thinking maybe an error trapping routine which then checks the error code and if it's a match it resumes (not Resume Next), if not then alert the user. Does this sound like the right way?
I have knocked this UNTESTED code up as I am not overly in bed with errors and error handling as I usually build my code to not error but in this case it is out of my control.
ErrHandler:
If Err.Number = -2147467259 Then
ErrorCount = ErrorCount + 1 'This is set to 0 at the start of the code
If ErrorCount > 5 Then
MsgBox "5 Rowfetch errors occured, could be a wider issue"
End
End If
Resume
End If
Err.Raise 'I Think this is wrong, how do I raise an error as VBA normally would?
I figured it out:
ErrHandler:
If Err.Number = -2147467259 Then
ErrorCount = ErrorCount + 1
If ErrorCount > 5 Then
MsgBox "5 Rowfetch errors occured, could be a wider issue"
End
End If
MsgBox "Stopped the error: " & ErrorCount 'In for testing to prove the error happened and was avoided
Resume
End If
Err.Raise Err.Number
This was the part I was not getting right:
Err.Raise Err.Number

ActiveWorkbook.Names.add seems to be incompatible with "Function"

I have a problem with :
Function Create_Model(adress As range, name As String) As String
Dim Msg As String
On Error GoTo ErrorHandler
ActiveWorkbook.Names.add "toto", "=Interface!$I$19"
Create_Model=name
Exit Function
ErrorHandler:
If Err.Number <> 0 Then
Msg = "Error # " & Str(Err.Number) & " was generated by " _
& Err.Source & Chr(13) & "Error Line: " & Erl & Chr(13) & Err.Description
MsgBox Msg, , "Error", Err.HelpFile, Err.HelpContext
End If
Resume Next
End Function
Indeed, if I run this, I get :
"Error # 1004 was generated by VBAProject
Error Line: 0
Application-defined or object-defined error"
It seems that the problem come from the use of "Function" because if I try to execute this with a "Sub", it's working.
Someone could please explain me why I can't do this with a "Function" and how I could replicate this function otherwise?
P.S : If I compile using Debug->Compile VBAProject. I don't get any message.
P.S.2 : This function aims to be used in excel formula.
P.S.3 : Argument used are : adress = J18:L20 and name = "Test". And finnaly, I would like replace "toto" by name and "=Interface!$I$19" by adress.
Thanks for your help.
After the comments below, I got to thinking, and realized that my original answer (see below) is perhaps more based in (my own!) best practice than being technically correct.
The main difference between a Function and a Subroutine is that a Function can return a value, while a Subroutine cannot.
I have heard of other issues in using Function rather than Subroutine but can't find a list right now. You have apparently found one, though! These (perhaps) rumors of issues, is why I tend to limit actions to Subroutines, and returning values to Functions.
After doing some digging, I've found the following resources that may help with further explanation of the difference between the two.
From Chip Pearson's site: a page called Macros and Functions
From ExcelFunctions.net: a page called Excel VBA Tutorial Part 4 - VBA Functions & Subroutines
Looks like I learned something today.
==ORIGINAL ANSWER==
Short answer and super high-level
Functions are typically not the best place to perform actions (on a worksheet, cell, file, create objects, etc). They are really good at returning values, and that is what they are designed for.
Subs or procedures are built for taking action.
I have never seen this particular issue, but have run into many things that trying to do them in a Function causes problems.

Stuck with some weird error in VBA

I am doing project in powerpoint 2007 automation. In that i am using macro programming (VBA). I am getting the following error while run the macro.
Err.Number= -2147024809 (80070057)
but my concern is not with error Because i want to catch this error and base on this error i want to perform some action.
Thats why i try to code like this for error handing code:
OnError Goto Err:
'some code
Err:
If Err.number = -2147024809 (80070057) then
'do something
end if
Resume next
So bascially if i write error number in this way then its not allow it. it gives error.
and major thing is when error occurs sometime it did not go to "Err : ". It just pop-up error with "End" and "Debug" options.
While it seems to work, I'd be cautious about using a reserved object name (Err) as a label, for one thing.
On Error GoTo ErrorHandler
' Your code here
' Make sure you don't hit the errorhandler when there's
' no error:
NormalExit:
Exit Sub ' or Function, whichever this is
ErrorHandler:
If err.Number = 123456788 Then
' Take corrective action
' then continue
Resume Next
End If
' Trap other specific or general errors here
' Then make sure you know where you're going to exit:
Resume NormalExit
If you need to trap very specific errors that might occur only at certain places in your code, you can also do a local errorhandler:
On Error Resume Next
' Some code that might cause problems
' Did it throw the error you're trying to trap?
If Err.Number = 12398745 then
' Corrective action
End If
' And now we return to our regularly scheduled error trapping
On Error GoTo ErrorHandler
Error numbers are numeric: -2147024809, its just displayed to you as the string "-2147024809 (80070057)" for clarity (80070057) being the error number in hexadecimal.
You want;
if err.number = -2147024809 then ....
or if you so choose
if err.number = &h80070057 then ....
The 80070057 part of the error message is the unsigned hexadecimal version of the negative number -2147024809. Remove that part of your code and you will be fine, if you want to keep track of the hex version of the number (can be useful for researching errors via Google etc) then just add it as a comment.