How to set a variable referring to the library database - vba

Currently I'm working on a library database for all my Access apps which have common code and forms in it. It is going quite well, but the only thing I cannot discover is how to set a variable referring to the reference. The library name is set by a constant and at startup is checked if the file exists in the app folder and if this is equal to the path stored in the reference.
However, I want to refer to the library with a short variable "dbl", because I don't want to use the library name every time. This name might change as well. How do I set the variable to do this? I have tried Set dbl = Application.References(APP_LIB), and this is accepted as valid, but I cannot call any routine or form with this variable.
Thx!
Art.
Public Function CkLib()
'Needs to stay in the App Module!
Dim ref As Reference
Dim strLib As String
On Error GoTo Err_Proc
For Each ref In Application.References
If ref.Name = APP_LIB Then
strLib = CurrentProject.Path & "\" & ref.Name & "." & APP_LIB_TYPE
If Dir(strLib, vbNormal) = "" Then 'File does not exist
MsgBox "The app library " & APP_LIB & " is missing. Please reinstall the app or ask for support!", vbCritical + vbOKOnly, APP_NAME & " App Error"
DoCmd.Quit acQuitSaveNone
Else
If ref.FullPath <> strLib Then 'Path needs to be updated
References.Remove ref
References.AddFromFile strLib
End If
Exit For
End If
End If
Next ref
If Nz(strLib, "") = "" Then
'Try to add ref
If Dir(CurrentProject.Path & "\" & APP_LIB & "." & APP_LIB_TYPE, vbNormal) <> "" Then
References.AddFromFile strLib
Else
MsgBox "Missing reference to app library " & APP_LIB & "! Please reinstall the app or ask for support!", vbCritical + vbOKOnly, APP_NAME & " App Error"
DoCmd.Quit acQuitSaveNone
End If
End If
Set dbl = Application.References(APP_LIB)
Exit_Proc:
Exit Function
Err_Proc:
Select Case Err.number
Case Else
MsgBox "Error: " & Trim(Str(Err.number)) & vbCrLf & _
"Desc: " & Err.description & vbCrLf & vbCrLf & _
"Module: Mod_Generic" & vbCrLf & _
"Function: CkLib", _
vbCritical, "Error!"
End Select
Resume Exit_Proc
End Function

Related

Find mailfolder in Outlook with Redemption

I try to find a folder in an Outlook account (I use Multiple accounts) using VBA and Redemption by using the FIND method but I cannot get it to work. On the Redemption webpage there is a reference made to an example and this may help but unfortunately the example isn't there.
Here's my code so far:
Public Function FindFolderRDO(strCrit As String) As String
If Not TempVars![appdebug] Then On Error GoTo Err_Proc
Dim objRdoSession As Redemption.RDOSession
Dim objRdoFolder As RDOFolder
Dim strFoundFolder As String
Dim objFoundFolder As RDOFolder
Dim strFolderName As String
Set objRdoSession = CreateObject("Redemption.RDOSession")
objRdoSession.Logon
objRdoSession.MAPIOBJECT = Outlook.Session.MAPIOBJECT
strFolderName = "\\[mailbox name]\[foldername]\[foldername]" 'actual names removed
Set objRdoFolder = objRdoSession.GetFolderFromPath(strFolderName)
Debug.Print objRdoFolder.Parent.Name 'Prints the folder name
Set objFoundFolder = objRdoFolder.Folders.Find("LIKE 'strCrit%' ") 'Does not work
Debug.Print objFoundFolder.Name
strFoundFOlder = objRdoFolder.Folders.Find("LIKE 'strCrit%' ") 'Does not work
Debug.Print strFoundFOlder
Exit_Proc:
On Error Resume Next
Set objRdoFolder = Nothing
Set objRdoSession = Nothing
Set objFoundFolder = Nothing
Exit Function
Err_Proc:
Select Case Err.Number
Case Else
MsgBox "Error: " & CStr(Err.Number) & vbCrLf & _
"Desc: " & Err.Description & vbCrLf & vbCrLf & _
"Source: " & Err.Source & vbCrLf & _
"Library: " & Application.CurrentProject.Name & vbCrLf & _
"Module: Mod_RDO" & vbCrLf & _
"Function: FindFolderRDO" & vbCrLf, _
vbCritical, "Error"
End Select
Resume Exit_Proc
End Function
Purpose of this function is to find a subfolder (can be up to 4 dimensions deep) having an unique case number of 6 numbers (for example "200332") on the first 6 positions. This function should provide NULL if not found or the full path and the name of the found folder.
I can create the full path with a seperate function (calling the parent folder until top level) but maybe there is a procedure in Redemption such as "fullpath" which I overlooked.
Eventually I want to use this function to delete, move or rename the mailbox folder.
My main question is how to use the "Find(Filter)" method. But any reply on the full path is welcome as well.
Thx! Art.
You are you trying to find a suborder with a name that starts with "strCrit"?
You are almost there:
Set objFoundFolder = objRdoFolder.Folders.Find("Name LIKE 'strCrit%' ")

MS Access VBA Open a Text file and write to a specific line without overwriting the file

I have a text file that I would like to add a header and a footer to. I don't want to overwrite the first or last lines, rather I'd like to add a new first line and append a line to the end of the file.
The below Function works for appending to the bottom of the file but I'd like to be able to control where the line is inserted. Thank you!
Function WriteToText(sFile As String, sText As String)
On Error GoTo Err_Handler
Dim iFileNumber As Integer
iFileNumber = FreeFile ' Get unused file number
Open sFile For Append As #iFileNumber ' Connect to the file
Print #iFileNumber, sText ' Append our string
Close #iFileNumber ' Close the file Exit_Err_Handler:
Exit Function Err_Handler:
MsgBox "The following error has occured" & vbCrLf & vbCrLf & _
"Error Number: " & Err.Number & vbCrLf & _
"Error Source: Txt_Append" & vbCrLf & _
"Error Description: " & Err.Description & _
Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _
, vbOKOnly + vbCritical, "An Error has Occured!"
GoTo Exit_Err_Handler End Function
What you do for a task like this:
Read the whole file into a string (Open For Input)
Add the data you want: S = "header line" & vbCrLf & S & vbCrLf & "footer line"
Write the whole string to the file, overwriting it (Open For Output)

How can I manage error code SQL in MS access form database?

I want manage SQL server error code in access form
sample duplicate error from SQL server
In Access VBA, you need to use:
On Error GoTo Error_Handler
' YOUR CODE HERE
.
.
.
Return_Label:
Exit Function
Error_Handler:
'What goes here depends on the data access model
Resume Return_Label
You may have to retrieve the Errors collection of the Error object as described here.
It shows this example code:
Sub DescriptionX()
Dim dbsTest As Database
On Error GoTo ErrorHandler
' Intentionally trigger an error.
Set dbsTest = OpenDatabase("NoDatabase")
Exit Sub
ErrorHandler:
Dim strError As String
Dim errLoop As Error
' Enumerate Errors collection and display properties of
' each Error object.
For Each errLoop In Errors
With errLoop
strError = _
"Error #" & .Number & vbCr
strError = strError & _
" " & .Description & vbCr
strError = strError & _
" (Source: " & .Source & ")" & vbCr
strError = strError & _
"Press F1 to see topic " & .HelpContext & vbCr
strError = strError & _
" in the file " & .HelpFile & "."
End With
MsgBox strError
Next
Resume Next
End Sub

VB6 Outlook automation

I have a block of code that will send emails using Outlook on the users PC. However when we email a bigger amount, it seems as though outlook isn't working as fast as the application so the application opens outlook sends the first email, but the second time it tries to open outlook but gets errors such as Outlook not available etc. So Outlook is taking to long to do the task while the application is trying to create the object again. I was using DoEvents previously but that doesn't work. Is there anyway to wait for outlook to finish its job Before it continues?
In this scenario outlook is not open yet, it is closed and the vb6 application is opening it.
You can trap Err.Number or Err.Description and generate a message box for the user to click to try again (Resume) or cancel (Exit Sub).
You may loop continually, attempting to create, to avoid user intervention. At some point generate the message box so users know the app is still working.
Edit 2015 05 06 - Maybe something less abstract. VBA but should be generic enough for other languages.
Option Explicit
Private Sub errorHandler_429()
Dim uErrorMsg1 As String
Dim uErrorMsg As String
Dim errCount As Long
uErrorMsg1 = "Click OK to try again."
On Error GoTo ErrorHandler
restart:
' code that triggers an error here
Err.Raise 429 ' <-- For testing
'Err.Raise 430 ' <--- For testing
ExitRoutine:
Exit Sub
ErrorHandler:
Select Case Err.Number
Case 429
errCount = errCount + 1
uErrorMsg = Err.Number & ": " & Err.Description & " occurred " & errCount & " times."
Debug.Print uErrorMsg
If (errCount Mod 200) = 0 Then
uErrorMsg = uErrorMsg1 & vbCr & vbCr & _
"Error # " & Err.Number & " was generated by " & _
Err.Source & Chr(13) & Chr(13) & Err.Description
Debug.Print uErrorMsg
If MsgBox(uErrorMsg, vbOKCancel, "errorHandler_429", Err.HelpFile, Err.HelpContext) = vbOK Then
Resume restart
Else
Resume ExitRoutine
End If
Else
Resume restart
End If
Case Else
uErrorMsg = Err.Number & ": " & Err.Description
'Debug.Print uErrorMsg
MsgBox uErrorMsg, , "errHandler_429", Err.HelpFile, Err.HelpContext
Resume ExitRoutine
End Select
End Sub
Sub errHandler_Description()
' Where the error number is negative and inconsistent
Dim uErrorMsg1 As String
Dim uErrorMsg As String
Dim errCount As Long
Dim LErrDesc As String
uErrorMsg1 = "Click OK to try again."
On Error GoTo ErrorHandler
restart:
' code that triggers an error here
Err.Raise 429 ' <--- For testing
'Err.Raise 430 ' <--- For testing
ExitRoutine:
Exit Sub
ErrorHandler:
LErrDesc = Left(Err.Description, 51)
Debug.Print " LErrDesc: " & LErrDesc
Select Case LErrDesc
Case "ActiveX component can't create object"
errCount = errCount + 1
Debug.Print " errCcount: " & errCount
If (errCount Mod 200) = 0 Then
uErrorMsg = uErrorMsg1 & vbCr & vbCr & _
"Error # " & Err.Number & " was generated by " & _
Err.Source & Chr(13) & Chr(13) & Err.Description
'Debug.Print uErrorMsg
If MsgBox(uErrorMsg, vbOKCancel, "errHandler_Description", Err.HelpFile, Err.HelpContext) = vbOK Then
Resume restart
Else
Resume ExitRoutine
End If
Else
Resume restart
End If
Case Else
uErrorMsg = "This error has not been handled."
uErrorMsg = uErrorMsg & vbCr & vbCr & _
"Error # " & Err.Number & " was generated by " & _
Err.Source & Chr(13) & Chr(13) & Err.Description
Debug.Print uErrorMsg
MsgBox uErrorMsg, , "errHandler_Description", Err.HelpFile, Err.HelpContext
Resume ExitRoutine
End Select
End Sub

MSXML2.DOMDocument load function fails in VBA

I've been struggling with the below issue for a while now and couldn't find the solution yet.
There is an iShare page with an XML file that I want to download using VBA code, then later process the XML file and save into MS Access database.
I've been using the below code for about 4 years now, it worked perfectly without any issues. But suddenly it stopped working this week.
Any ideas why?
the code:
Private Function GetRequests() As Boolean
On Error GoTo ErrHandler
Dim oDoc As MSXML2.DOMDocument
Dim Url As String
Dim sFileName As String
Set oDoc = New MSXML2.DOMDocument
oDoc.async = False
Url = cUrlDatabase & "/" & cApplicationName & "/In/" & cReqXmlFile
UpdateStatus "Loading " & cReqXmlFile
If Not oDoc.Load(Url) Then
c_sLastError = "Could not load XML " & Url
GoTo EndProc
End If
sFileName = sPath & "\Data\requests.xml"
oDoc.Save sFileName
GetRequests = True
End Function
The code fails at the oDoc.Load(Url) part, it comes back false.
Here's an example of how to gather error details:
Dim xDoc As MSXML.DOMDocument
Set xDoc = New MSXML.DOMDocument
If xDoc.Load("C:\My Documents\cds.xml") Then
' The document loaded successfully.
' Now do something intersting.
Else
' The document failed to load.
Dim strErrText As String
Dim xPE As MSXML.IXMLDOMParseError
' Obtain the ParseError object
Set xPE = xDoc.parseError
With xPE
strErrText = "Your XML Document failed to load" & _
"due the following error." & vbCrLf & _
"Error #: " & .errorCode & ": " & xPE.reason & _
"Line #: " & .Line & vbCrLf & _
"Line Position: " & .linepos & vbCrLf & _
"Position In File: " & .filepos & vbCrLf & _
"Source Text: " & .srcText & vbCrLf & _
"Document URL: " & .url
End With
MsgBox strErrText, vbExclamation End If
Set xPE = Nothing
End If
Example taken from here.
For other people finding this post:
The xml parser by now has implemented different error types (see here).
You would have to use the following code
Set objXML = CreateObject("Msxml2.DOMDocument.6.0")
ObjXML.async=true
objXML.load "/path/to/xml"
If objXML.parseError.errorCode <> 0 Then
MsgBox "Error was " + objXML.parseError.reason
End If
This should help you debug your .xml file.
For anyone else struggling with this, I found this error to be caused by text encoded in a format which could not be parsed in VBA (some weird E symbol). The objXML was nothing after the .load. I'm sure there are many possible causes, but I'll share what I found in case this helps someone. Thanks to the guys above for the error handling routines.