MS Access VBA Reference - ms-access

I have this database and it was working perfect, but today it started hating me. I started getting a Compile error: Can't find project or library. I looked around and found that when using the front end on a different computer, under VBA and references, it was missing a reference for Microsoft Word 15.0 Object Library.
My computer I have the Microsoft Word 16.0 Object Library checked. How can I get this to work on other computers with 15.0 and 16.0?
Here is the code
Private Sub cmd_LocateFile_Click()
On Error GoTo Error_Handler
Dim sFile As String
Dim sFolder As String
Dim ID As Long
Dim sTarget As String
sFile = FSBrowse("", msoFileDialogFilePicker, "All Files (*.*),*.*")
If sFile <> "" Then
sFolder = ("\\aiowima23fp1\Ecological Sciences and Engineering\Cultural Resources\New - Cultural Resources Request Database") & "\" & sAttachmentFolderName & "\"
If FolderExist(sFolder) = False Then MkDir (sFolder)
ID = RequestID_FK ' Set current record id.
sTarget = sFolder & CStr(ID) & "-" & GetFileName(sFile)
If CopyFile(sFile, sFolder & GetFileName(sTarget)) = True Then
Me!FullFileName.Value = sTarget
Else
End If
End If
Error_Handler_Exit:
On Error Resume Next
Exit Sub
Error_Handler:
MsgBox "The following error has occured" & vbCrLf & vbCrLf & _
"Error Number: " & Err.Number & vbCrLf & _
"Error Source: " & sModName & "\cmd_LocateFile_Click" & vbCrLf & _
"Error Description: " & Err.Description & _
Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _
, vbOKOnly + vbCritical, "An Error has Occured!"
Resume Error_Handler_Exit
End Sub

Adding references to Word during development has the advantage to be able to use strongly typed word objects and thus being able to take advantage of IntelliSense. But these references are, as you have experienced, sensible to versioning. Therefore, I suggest you remove the reference to Word once development is achieved. You will also have to replace all Word-related types by Object, i.e. use late binding. This makes the application more robust with respect to versioning.
You can get an existing Word instance which is already open or open a new one with this code snippet, without any references to Word DLLs.
Public Function GetWordApplication() As Object
'Gets an active Word application or opens a new Word instance.
'Raises Error No. 8 if word cannot be opened.
On Error Resume Next
'Find existing instance of Word
Set GetWordApplication = GetObject(, "Word.Application")
If Err.Number <> 0 Then 'Not found, create new instance.
Set GetWordApplication = CreateObject("Word.Application")
End If
'Following code is optional. You can instead test for Nothing at the call site
On Error Goto 0
If GetWordApplication Is Nothing Then
Err.Raise 8, "YourApp.GetWordApplication", "Word could not be opened."
End If
End Function

Related

MS Access Linked Table Manager doesn't update new data source

I have an MS Access 2010 Database that has a table that is linked to a CSV file. Upating the CSV files location using the inbuilt Access "Linked Table Manager" doesn't work.
I check the file i want to update, choose "always prompt for new location" and select the new file. I get a message telling me that the update was successful, but when I go to check, the table is still linked to the old file.
Is this a MS Access bug and if so what is the most efficient workaround?
I ended up deleting the old table and manually recreating a new table with the same specifications.
*Updated: -- I forgot to include the referenced Function Relink_CSV :(
Yes, I would call it a bug. Microsoft probably calls it a 'design characteristic'.
As you have discovered, you can manually fix the issue. If you are interested in a code solution, then I may have something that will work for you -- if your CSV file is delimited by comma's.
The following code (which you need to modify!) will delete the existing linked csv file, then add a link to the same file. For debugging, my code then deletes that link and adds a link to a different file name, but in the same folder.
There are other solutions that make use of a saved Import Specification, that you can reuse, if your csv format is not simple.
Option Explicit
Option Compare Database
Sub Call_Relink()
Dim dbs As DAO.Database
Dim tdf As DAO.TableDef
Dim strTableName As String
Dim strPath As String
Dim strFile As String
Dim iReply As Integer
iReply = MsgBox("WARNING!!!! This code will remove the linked tables 'FileA' and 'FileB'" & vbCrLf & vbCrLf & _
"Click 'Yes' to Continue" & vbCrLf & "Click 'No' to Stop", vbYesNo, "CAUTION!! Will remove linked table(s)")
If iReply <> vbYes Then
Exit Sub
End If
On Error GoTo Error_Trap
Set dbs = CurrentDb
dbs.TableDefs.Delete "FileA" ' For testing; delete table if it already exists
strPath = "C:\Temp\"
strFile = "FileA.csv"
strTableName = "FileA" ' Table name in Access
Relink_CSV strTableName, strPath, strFile ' Call function to link the CSV file
dbs.TableDefs.Refresh ' Refresh TDF's
Debug.Print "Pause here and check file link" ' Put a breakpoint here; pause and look at the table in Access
dbs.TableDefs.Delete "FileA" ' For testing; delete table if it already exists
strPath = "C:\Temp\" ' Path to next csv
strFile = "FileB.csv" ' Name of next csv file
strTableName = "FileA" ' Table name in Access
Relink_CSV strTableName, strPath, strFile ' Call function to link to a different CSV file
dbs.TableDefs.Refresh
Debug.Print "Pause here and check file link" ' Put a breakpoint here; pause and look at the table in Access
My_Exit:
Set dbs = Nothing
Exit Sub
Error_Trap:
Debug.Print Err.Number & vbTab & Err.Description
If Err.Number = 3265 Then ' Item not found in this collection.
' Ignore this error
Resume Next
End If
MsgBox Err.Number & vbTab & Err.Description
Resume My_Exit
Resume
End Sub
Function Relink_CSV(strTableName As String, strPath As String, strFile As String)
' (1) Name of the table in Access
' (2) Path to the file
' (3) File name
On Error GoTo Relink_Err
DoCmd.TransferText acLinkDelim, , strTableName, strPath & strFile, False, ""
Relink_Exit:
Exit Function
Relink_Err:
Debug.Print Err.Number & vbTab & Err.Description
MsgBox Err.Number & vbTab & Err.Description
Resume Relink_Exit
Resume
End Function

How to reference control names dynamically in MS Access

I have the following code in an MS Access Form object.
Private Sub UpdatePMText(sLang As String)
'Used to pass both Mandate and Language Info to called Sub that will execute the queries
Dim iMandate As Integer
'Check if there is text in the box.
If Me.Controls("txtInput_PM_" & sLang & "_DRAFT") = Null Or Me.Controls("txtInput_PM_" & sLang & "_DRAFT") = "" Then
MsgBox ("There is no text in the " & sLang & " DRAFT Field." & vbNewLine & "The operation will not be executed.")
Exit Sub
End If
iMandate = Me.txtMandateID
Call modUpdateText.macro_PMText(iMandate, sLang)
End Sub
If I refer to the Controls directly and simply type out the names of the forms, for example txtInput_PM_EN_DRAFT then the code works as intended.
The error message I get is that Access can't find the "Field" I'm referring to when I am on the first IF statement line. I have tried changing the .Controls to .Fields but that didn't work either.
I do not want to repeat the same code for every language I need to run. How do I reference control names dynamically in MS Access? What am I missing?
I think you need to add some basic troubleshooting. The answer is probably simpler than you think. It's likely you're just trying to lookup a textbox with mispelled name or it's failing on the Null comparison (as suggested by #HansUp)
I would try modifying your basic sub and testing it with this subroutine. As long as your code is in your current form and you're not referencing a subform your method will work.
Private Sub UpdatePMText(sLang As String)
Const ERR_MISSING_CONTROL As Long = 2465
On Error GoTo Err_UpdatePMText
Dim sTextBox As String
sTextBox = "txtInput_PM_" & sLang & "_DRAFT"
'Check if there is text in the box.
If Nz(Me.Controls(sTextBox).Value, "") = "" Then
MsgBox ("There is no text in the " & sLang & " DRAFT Field." & vbNewLine & "The operation will not be executed.")
Exit Sub
End If
Exit Sub
Err_UpdatePMText:
If Err.Number = ERR_MISSING_CONTROL Then
MsgBox "Error: No Such Textbox: " & sTextBox
Else
MsgBox "Error Looking Up Textbox: """ & sTextBox & """" & vbCrLf & Err.Description
End If
End Sub

Sending Email with Late Binding Error Through VBA

I'm trying to use a late binding to email from VBA in Access 2010 and Access 2003. 2003 gives me 'Could not complete the operation. Once or more paramet values are not valid.' and 2010 gives me 'Invalid procedure call or argument.' I've done the step through and it fails at .send near the bottom. Am I setting up my binding wrong? I'm trying to do this without using Microsoft Object Library in the References.
Thanks.
'Refers to Outlook's Application object
Dim appOutlook As Object
'Refers to an Outlook email message
Dim appOutlookMsg As Object
'Refers to an Outlook email recipient
Dim appOutlookRec As Object
'Create an Outlook session in the background
Set appOutlook = CreateObject("Outlook.Application")
'Create a new empty email message
Set appOutlookMsg = appOutlook.CreateItem(olMailItem)
'Using the new, empty message...
With appOutlookMsg
strSQL = "SELECT Email FROM Employees WHERE " & sqlVar & " = True"
Set myR = CurrentDb.OpenRecordset(strSQL)
Do While Not myR.EOF
Set appOutlookRec = .Recipients.Add(myR!Email)
appOutlookRec.Type = olTo
myR.MoveNext
Loop
strSQL = "SELECT Email FROM Employees WHERE '" & user & "' = Username"
Set myR = CurrentDb.OpenRecordset(strSQL)
Set appOutlookRec = .Recipients.Add(myR!Email)
appOutlookRec.Type = olCC
.Subject = wonum
.Body = "Department: " & strDep & vbNewLine & vbNewLine & _
"Issue is at: " & strIssue & vbNewLine & vbNewLine & _
"Priority is: " & strPriority & vbNewLine & vbNewLine & _
"Complete by: " & strDate & vbNewLine & vbNewLine & _
"Description: " & strDesc
.Send
End With
Without a reference, VBA will not know about Outlook constants such as olTo and olCC. With late binding (no reference), you must supply the values for constants rather than the names of the constants.
However, since you didn't report a compile error about those constants, that suggests your code module does not include Option Explicit in its Declarations section. Trying to troubleshoot VBA code without Option Explicit is a waste of time.
Add Option Explicit, then choose Debug->Compile from the VB Editor's main menu and fix anything the compiler complains about. Resume you troubleshooting afterward.
There is an article here on sending email via Outlook using early and late binding. In the "Late Bound Conversion Checklist" at the end, the last suggestion is
Add optional arguments that have a default value
I cannot vouch for that advice because when I need to send email messages from Access I use CDO, not Outlook. Still, it sounds like it might be worth a try.

ODBC call failed error when an access table is opened

I have linked the sql server tables to ms access so that I can use ms access as the front end.I was able to access the tables from access, until I run into an error ODBC call failed when I tried to open one of the tables. There was no problem with the other tables. Actually I have changed a column name in sql server after creating a link. Is this the problem? I am really worried about this as I was about to use access as a front-end for my future purposes.
When you link to a remote table, Access stores metadata about that table. When you later change the table structure, the metadata doesn't get updated to capture the change.
Delete the link. Then recreate the link. That way the metadata will be consistent with the current version of the table.
Yes changing the column name after linking the table is most likely causing your failure. Is it is now trying to pull data from a column that no longer exists. You will need to relink the table. You can programatically link tables in access. We do that in may of our access applications and drive the tables that need to be linked from a local access table.
Public Sub LinkODBCTables()
Dim objRS As DAO.Recordset
Dim objTblDef As DAO.TableDef
Dim strTableName As String
Dim strAliasName As String
Dim strDSN As String
Dim lngTblCount As Long
Set objRS = CurrentDb.OpenRecordset( _
" select TableName," & _
" AliasName," & _
" DSN," & _
" DatabaseName," & _
" Development_DSN," & _
" UniqueIndexCol" & _
" from tblODBCLinkedTables " & _
" order by TableName", dbOpenSnapshot)
While Not objRS.EOF
' Check to see if we already have this linked tableDef
' We don't care if it is not actually in there
strTableName = objRS.Fields("TableName")
If Not IsNull(objRS.Fields("AliasName")) Then
strAliasName = objRS.Fields("AliasName")
Else
strAliasName = strTableName
End If
If DEV_MODE Then
strDSN = objRS.Fields("Development_DSN")
Else
strDSN = objRS.Fields("DSN")
End If
On Error Resume Next
CurrentDb.TableDefs.Delete strAliasName
If Err.Number <> 0 And _
Err.Number <> 3265 Then ' item not found in collection
Dim objError As Error
MsgBox "Unable to delete table " & strAliasName
MsgBox Err.Description
For Each objError In DBEngine.Errors
MsgBox objError.Description
Next
End If
On Error GoTo 0
Set objTblDef = CurrentDb.CreateTableDef(strAliasName)
objTblDef.Connect = g_strSQLServerConn & _
"DSN=" & strDSN & _
";DATABASE=" & objRS.Fields("DatabaseName") & _
";UID=" & g_strSQLServerUid & _
";PWD=" & g_strSQLServerPwd
objTblDef.SourceTableName = strTableName
On Error Resume Next
CurrentDb.TableDefs.Append objTblDef
If Err.Number <> 0 Then
Dim objErr As DAO.Error
For Each objErr In DBEngine.Errors
MsgBox objErr.Description
Next
End If
On Error GoTo 0
' Attempt to create a uniqe index of the link for updates
' if specified
If Not IsNull(objRS.Fields("UniqueIndexCol")) Then
' Execute DDL to create the new index
CurrentDb.Execute " Create Unique Index uk_" & strAliasName & _
" on " & strAliasName & "(" & objRS.Fields("UniqueIndexCol") & ")"
End If
objRS.MoveNext
Wend
objRS.Close
End Sub
We are using a single SQLServer login for our access applications so the g_strSQLServerUID and g_strSQLServerPwd are globals that contain that info. You may need to tweek that for your own needs or integrated security. We are setting up two DSNs one for production and the other for development. The DEV_MODE global controls wich set of DSNs are linked. You can call this code from a startup macro or startup form. It will deleted the old link and create a new link so you always have the most up to date schema.

Import data from web site in MS Access

I want to write an import function for importing data from a website into a ms access database. The original data is stored in a mysql database and is available via a remote text file (csv). So all I want is to access this remote file and read it in vba. The code I use is below, but it seems that references are missing. Don't know which reference ActiveXperts.Http needs. Could anybody help?
Thx
Steve
Dim objHttp
Dim strUrl
Dim strData
' Create a HTTP instance
Set objHttp = CreateObject("ActiveXperts.Http")
' Write some information to console
MsgBox "ActiveSocket " & objHttp.Version & " demo."
MsgBox "Expiration date: " & objHttp.ExpirationDate & vbCrLf
Do
strUrl = InputBox("Enter a URL", "Input", "www.activexperts.com/products")
Loop Until strUrl <> ""
objHttp.LogFile = "C:\HttpLog.txt"
objHttp.Connect (strUrl)
If (objHttp.LastError <> 0) Then
MsgBox "Error " & objHttp.LastError & ": " & objHttp.GetErrorDescription(objHttp.LastError)
Else
strData = objHttp.ReadData
If (objHttp.LastError <> 0) Then
MsgBox "Error " & objHttp.LastError & ": " & objHttp.GetErrorDescription(objHttp.LastError)
Else
MsgBox strData
End If
objHttp.Disconnect
MsgBox "Disconnect."
End If
MsgBox "Ready."
This tutorial says that it needs ActiveSocket 2.4 Type Library.
(the tutorial is for VB 5/6 and not VBA, but the reference stuff should be exactly the same)