Hey guys, so here's my latest issue. I have a database where I would like to send emails from outlook. So I had a read around and found out how to create your own buttons with vba in outlook. Which is great so I set to work on this. The code below is initiated when a user form opens .
Code:
Dim dbs As DAO.Database
Set dbs = DAO.OpenDatabase("c:\mrl\frontend.accdb")
Dim i As Integer
Dim rst As DAO.Recordset
Set rst = dbs.OpenRecordset("SELECT [Contact ID], [First Name], [Last Name] FROM Contacts")
rst.MoveFirst
i = 0
With Me.cmb
.Clear
Do
If Not IsNull(rst.Fields("[Contact ID]")) Then
.AddItem
.List(i, 2) = rst.Fields("[Contact ID]")
.List(i, 1) = Nz(rst.Fields("[First Name]"), "")
.List(i, 0) = Nz(rst.Fields("[Last Name]"), "")
i = i + 1
rst.MoveNext
End If
Loop Until rst.EOF
End With
Set dbs = Nothing
Set rst = Nothing
Ok great that work, now I have a combobox with the names and id numbers of all of the clients from the database. That's all good. The next thing was to then use this code (below) on a button to tell it to store the details of the selected email as variables. It then passes the content of the email to another function which removes the special characters and passes the result back to the variable (this stops any issues with SQL later). After the user has selected a name from the list, they press the button and it creates a new entry in the database with the details of the email...
Code:
Dim olItem As Outlook.MailItem
Dim sText As String
Dim sSubject As String
Dim sFrom As String
Dim sTo As String
Dim sDate As Date
Dim strSQL As String
Dim dbs As DAO.Database
Set dbs = DAO.OpenDatabase("c:\mrl\frontend.accdb")
Set olItem = ActiveExplorer.Selection.Item(1)
sText = olItem.Body
sSubject = olItem.Subject
sFrom = olItem.SenderEmailAddress
sTo = olItem.To
sDate = olItem.SentOn
'This section of code removes all of the special characters from the email content to stop it messing with the SQL code.
ValidateString sText
strSQL = "INSERT INTO SavedEmails ([Sender Name], [Received], [Subject], [Content], [ClientID]) VALUES ('" & sFrom & "', '" & sDate & "', '" & sSubject & "', '" & sText & "', '" & cmb.Column(2) & "')"
dbs.Execute strSQL
Set dbs = Nothing
Set rst = Nothing
Ok, still with me? Well that too works fine, however there is an issue, I cant do this while the database is open as it says the database has been placed in a state blah blah, its obviously open and is read only.
I suppose my question is this, is there are way to do this with the database open? If not, is there any way to tell it to close access when its done because at the moment it leaves access running in the background and wont allow me to open the database until I force the background process (msaccess.exe) to close.
As always any and all help really is greatly appreciated.
P.s just so you understand my thinking here, the other route I could have taken is to import the emails from the inbox and then assign them from the database but this is clumsy and means when I have read an email I think is important to a certain client in the database I would then have to go to the database, find the email and then assign it and I don't want that.
Many, many thanks in advance guys, I really do appreciate it!