I'm trying to write a simple little routine to email an attachment stored in an Access 2007 database. For some reason I cannot get the simplest part of it to work.
I get an error saying "User-defined type not defined" on the following line:
Dim attachmentField As DAO.Field2
Now up to this point I haven't accessed any DAO objects yet, but my assumption was that I only needed to add the relevant reference. Thing is, I seem to have a misconception about what that reference is. I have tried "Microsoft DAO 3.6 Object Library" which made sense, but I'm still getting the same error message. Then I tried 3.5 of the same and then JET and then a few more that made far less sense.
Here's the full listing, in case I missed something else that is real basic. I know it needs an awful lot of cleanup, but I'd like to get it working first.
Private Sub Command4_Click()
Dim appOutLook As Outlook.Application
Dim MailOutLook As Outlook.MailItem
Set appOutLook = CreateObject("Outlook.Application")
Set MailOutLook = appOutLook.CreateItem(olMailItem)
With MailOutLook
.To = Description.Value
.Subject = "Confirmation of " & ID.Value
'Error on the next line
Dim attachmentField As DAO.Field2
attachmentField = Recordset("Att")
attachmentField.SaveToFile "C:\Temp\" & Att.FileName
Attachments.Add "C:\Temp\" & Att.FileName, olByValue, 1, "Document"
'.DeleteAfterSubmit = True
.Send
End With
End Sub
Any ideas?
You need a reference to DAO Version 12 - which supports the new FIELD2 object
Try adding this reference - "Microsoft Office 12.0 Access database engine"
Change the line to
Dim attachmentField As DAO.Field
Also, where does the Recordset come from? Where is it being filled with records?
Related
I read through a pretty thorough response in the link How to search through VBA code files and it works just fine for the current project. However, I'm just feeling slow in opening up other projects and looking through their code.
The response mentioned using OpenDatabase but I'm not seeing examples about the association between the database and the Application.VBE.ActiveVBProject. I've not been lazy about this, but 4 days of searching the web has exhausted my options.
Any help would be really appreciated.
My apologies. Found other way to make this work.
Public Sub FindWordInOtherModules(ByVal pSearchWord As String, sApplicationFilePath As String)
Dim objComponent As VBComponent
' VBComponent requires reference to Microsoft Visual Basic
' for Applications Extensibility; use late binding instead:
Dim lStartLine As Long
Dim lEndLine As Long
Dim lStartColumn As Long
Dim lEndColumn As Long
Dim accApp As Access.Application
Set accApp = New Access.Application
With accApp
.Visible = True
.OpenCurrentDatabase (sApplicationFilePath)
.UserControl = True
'MsgBox .VBE.ActiveVBProject.VBComponents.Count
'MsgBox .CurrentDb.Name
For Each objComponent In .VBE.ActiveVBProject.VBComponents
If objComponent.CodeModule.Find(pSearchWord, lStartLine, lStartColumn, lEndLine, lEndColumn, _
FindWholeWord, MatchCase, PatternSearch) = True Then
MsgBox "Found text " & StringToFind & vbCrLf _
& "Start line: " & lStartLine & vbCrLf _
& "Line text: " & objComponent.CodeModule.Lines(lStartLine, lEndLine - lStartLine + 1), vbOKOnly, objComponent.CodeModule.Name
End If
Next objComponent
End With
accApp.CloseCurrentDatabase
Set accApp = Nothing
End Sub
You should probably add accApp.Quit:
accApp.CloseCurrentDatabase
accApp.Quit
Set accApp = Nothing
before Set accApp = Nothing to speed up closing the application and close it during execution of this code (Public Sub FindWordInOtherModules), on the line accApp.Quit, not later. On my computer mouse is still inactive several seconds after execution such kind of Sub if accApp.Quit is not added.
But there is no need to open another database, because the current database can be only "linked' to it by creating temporary reference:
Private Sub FindWordInOtherModules2()
Dim objComponent As VBComponent
...
...
Dim lEndColumn As Long
Dim ref As Reference
Dim RefName As String
Const FileName = "C:\Users\....mdb"
With Application 'instead of accApp
.References.AddFromFile FileName
'.References.Count because the new one is supposed be the last one (?)
RefName = .References(.References.Count).Name
Dim VBProj As VBProject
For Each VBProj In .VBE.VBProjects
If VBProj.FileName <> .CurrentDb.Name Then Exit For
Next
For Each objComponent In VBProj.VBComponents
'Debug.Print objComponent.Name
...
...
Next
Set objComponent = Nothing '?
Set VBProj = Nothing '?
Set ref = .References(RefName)
.References.Remove ref
Set ref = Nothing '??
End With
End Sub
This seems be faster then opening another database file, but VBA can't be updated.
References.Remove ref removes reference, but VBA folders are still visible in the left panel and all the code works, what is a little disturbing ...
Application.VBE.VBProjects.Remove VBProj doesn't work. It may have something to do with "Trust access to the VBA project object model" option in Trust Center - Macro Settings, which is not available in Access.
But the project is not visible after closing and opening the database.
I'm trying to link two tables in different databases. What I've done is create a new table and then try to change the DESCRIPTION property to a path of a specific table in the other Database.
Set dbs = CurrentDb
thepath = "DATABASE=P:\Cadworx P&ID Implementation\3 Piping\P&IDs Jesus Test\Testproject\myTest.mdb;TABLE=Service"
Set tdf = dbs.TableDefs("ThisTable")
On Error Resume Next
tdf.Properties("Description") = "DATABASE=P:\Cadworx P&ID Implementation\3 Piping\P&IDs Jesus Test\Testproject\myTest.mdb;TABLE=Service"
If Err.Number = 3270 Then
Set prp = tdf.CreateProperty("Description", _
dbText, thepath)
tdf.Properties.Append prp
End If
This hasn't given me the desired results, as the link is never established. Can someone please tell me if this is the right way to do this or if there is a better way? Thanks for your help.
You can link to tables from other Access databases fairly easily using Access' in-built features.
Depending on the version of Access you're using, there should be an option to import/link to other data sources, including another Access database:
This starts an import/link wizard. Browse to the database file you want to link to:
Specify whether you want to import or link to data objects within the database file you browsed to (in this case, link):
Select the objects you want to link to:
Linked objects will appear in your navigation pane with a blue arrow denoting they are a linked object, rather than something stored locally to your current database:
You should then be able to use your linked tables as if they were any other kind of table in your database.
Here is the solution to the problem.
Dim dbs As DAO.Database
Dim tdf As DAO.TableDef
Dim strDbFile As String
Dim strConnect As String
Dim strSourceTableName As String
Dim strLinkName As String
Dim f As Object
Dim varfile As Variant
Set f = Application.FileDialog(3) 'Windows Explorer, 3 means file picker. 4 Is for folder, 1 to open dialog box, 2 to save as
f.show 'Show Windows explorer
With f
f.allowmultiselect = False
For Each varfile In .selecteditems
strDbFile = varfile 'File selected assigned to source database file
Next
End With
strSourceTableName = "Service" 'Source Table
strLinkName = "Service" 'Final table name once link has been established
strConnect = "MS Access;PWD=" & strPassword & ";DATABASE=" & strDbFile
Set dbs = CurrentDb
Set tdf = dbs.CreateTableDef
tdf.Connect = strConnect 'Establish link between databases
tdf.SourceTableName = strSourceTableName
tdf.Name = strLinkName
dbs.TableDefs.Append tdf
As Gustav said, the solution was along what he posted. New to access so I didn't really know what the DAO library is. Thanks for your help anyway!
How can I get current Office Windows account email using VBA code?
I do not mean the account which the user is logged in the Windows, I mean the account that is authorized in office
See image:
I don't believe you can access it. Your best bet is linking Access to Outlook and trying to access it from there.
For example you Set a reference to the Outlook object library and then :-
Dim olook As Outlook.Application
Dim EAddress As String
Set olook = GetObject(, "Outlook.Application")
Set olook = CreateObject("Outlook.Application")
EAddress = olook.Session.CurrentUser.Address
I have a similar solution calling out to Outlook, I'm using Excel and found a way to do this, I've only ever found one address in the Accounts collection, but have a suffix match to try and catch the #company.com I'm looking for:
Dim NameSpace As Object
Dim strEmailAddress As String
Set NameSpace = CreateObject("Outlook.Application").GetNameSpace("MAPI")
strEmailAddress = ""
For Each Account In NameSpace.Accounts
If LCase(Split(Account.SMtpAddress, "#")(1)) = "contoso.com" Then
strEmailAddress = Account.SMtpAddress
Else
strEmailAddress = "Unknown"
End If
' If you want to see more values, uncomment these lines
'Debug.Print Account.DisplayName
'Debug.Print Account.UserName
'Debug.Print Account.SMtpAddress
'Debug.Print Account.AccountType
'Debug.Print Account.CurrentUser
Next
Outlook interrupts the VBA-execution (to access Outlook objects from macro) due to security.
Snap shot
Outlook Security
Hence only to get eMailID without opening object as well as handling error in case of non availability of outlook/account, following code can work in your case
VBA Code
Sub Email_Address()
Dim MAPI As Object
Status = "unknown"
On Error GoTo return_value
Set MAPI = CreateObject("Outlook.Application").GetNameSpace("MAPI")
i = 1
Do While True
Debug.Print MAPI.Accounts.Item(i)
i = i + 1
Loop
return_value:
If i > 1 Then: Status = "done..."
Debug.Print Status
End Sub
Fish out of water looking for direction/advice not necessarily code (Although that would be helpful). I am an ME that designs machines for a living and I am trying to automate an existing 2010 Access database used for engineering part numbers. I want the user to select the project number and have the next available part number automatically appear in the part number field.
I have tried unsuccessfully using ADO and DAO examples from the various sources to pass a parameter that is based on the value of a form control to a select query. My latest attempt is as follows:
Private Sub ctlProject_AfterUpdate()
Dim dblProject As Double
dblProject = Me.ctlProject.Value
MsgBox "The curret project number is " & dblProject, vbOKOnly, "Project Number"
Const cstrQueryName As String = "qryDetails"
Dim dbs As DAO.Database
Dim qdf As DAO.QueryDef
Dim rst As DAO.Recordset
Set dbs = CurrentDb()
Set qdf = dbs.QueryDefs(cstrQueryName)
qdf.Parameters("Project") = dblProject
' Open recordset on the query
Set rst = qdf.OpenRecordset()
rst.MoveLast
Debug.Print ("Project ID: " & rst!Project)
rst.Close
qdf.Close
dbs.Close
End Sub
Code produces a "Run-Time Error '3265'. Item not found in this collection" that stems from this line of code:
qdf.Parameters("Project") = dblProject
Original Source code here
I have had similar failures using the ADO Command object. I would greatly appreciate any help or recommendations for references/resources. I have been using "Access 2003 vba programmers's manual" by WROX and "Access 2010: the missing manual" as hardcopy references.
Here is the code from the SQL view of qryDetails:
SELECT tblDetails.Project, tblDetails.Number, tblDetails.Title, tblDetails.Initials,
tblDetails.IssuedOn
FROM tblDetails
WHERE (((tblDetails.Project)=[Project]));
I'm skeptical about Project as the parameter name because there is a field with the same name. It's also a reserved word, but I'm unsure whether that is an extra challenge here.
Revise qryDetails to use a different name for the parameter.
SELECT tblDetails.Project, tblDetails.Number, tblDetails.Title, tblDetails.Initials,
tblDetails.IssuedOn
FROM tblDetails
WHERE (((tblDetails.Project)=[pProject]));
And don't forget to revise the VBA to use that new name ...
qdf.Parameters("pProject") = dblProject
If you still have trouble after those changes, see what Access thinks about your query parameter(s).
Const cstrQueryName As String = "qryDetails"
Dim dbs As DAO.Database
Dim prm As DAO.Parameter
Dim qdf As DAO.QueryDef
Set dbs = CurrentDb()
Set qdf = dbs.QueryDefs(cstrQueryName)
MsgBox "Parameters.Count: " & qdf.Parameters.Count
For Each prm In qdf.Parameters
MsgBox "Parameter Name: " & prm.Name
Next
I am fairly new to VBA but am trying to upload data from an Excel Workbook to an Access database table from a computer that does not have Access installed. I have searched for a solution online but haven't found anything yet that I can get to work with my code.
The error code I am getting is...429 cannot create activex component
I have some VBA code set up in the Excel workbook which calls a Sub in Access [which works on a machine which has Access installed] but I don't know what the correct code should be if the machine doesn't have Access installed.
Sub Upload_SiteObsData_Excel_To_Access(Database_Path)
Database_Path = "\\Path\db1.mdb"
Dim acApp As Object
Dim db As Object
Set acApp = CreateObject("Access.Application")
acApp.OpenCurrentDatabase ("\\Path\db1.mdb")
Set db = acApp
acApp.Run "Upload_SiteObsData_to_Access"
acApp.Quit
Set acApp = Nothing
End Sub
The procedure in Access is as follows:
Option Compare Database
Option Explicit
Dim Excel_Path As String
Dim Excel_Range As String
Dim UserNameOffice As String
Dim Excel_File_TechForm As String
Sub SetUp_Variables()
UserNameOffice = CreateObject("wscript.network").UserName
Excel_Path = "C:\Documents and Settings\" & UserNameOffice & "\Desktop\"
Excel_Range = "MyData"
Excel_File_TechForm = "SiteObsForm_v0.2.xls"
End Sub
Sub Upload_SiteObsData_to_Access()
SetUp_Variables
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, "TBL_SiteObsData", Excel_Path & Excel_File_TechForm, True
End Sub
I would be extremely grateful for any help. Thanks in advance
I was just fooling around with some Excel VBA code and the following seemed to work:
Option Explicit
Sub Upload_Excel_to_Access()
Dim con As Object '' ADODB.Connection
Set con = CreateObject("ADODB.Connection") '' New ADODB.Connection
con.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data source=C:\Users\Public\Database1.accdb;"
con.Execute _
"INSERT INTO TBL_SiteObsData " & _
"SELECT * FROM [Excel 12.0 Xml;HDR=YES;IMEX=2;ACCDB=YES;DATABASE=C:\Users\Public\Book1.xlsm].[Sheet1$]"
con.Close
Set con = Nothing
End Sub
I think you'l have to find another way round this issue, without access installed, Excel cannot create the "Access.Application" object, it just flat-out doesn't know what access is.
Can you pull the data from Access instead?
I've done this the first time I connected Excel to Access via VBA, and I know for sure it works using the code that Gord Thomson provided. Establishing an ADODB connection and executing an append query.