I am working on an Access DB that sorts and tracks my firm's business contacts. We have a form called "Contact Profile" whereby the user can select a given contact and view all of his information: First Name, Last Name, Company, Title, Email Address, etc.
On the profile, the user can update a contact's information with the "Update Info" button.
Every single field updates just fine--with the exception of Email Address. For example, I can change Joseph Smith, Programmer at Google to Joe Smith, Program Manager at GOOG with no issues.
But if I try to change joesmith#google.com to jsmith#google.com, the change does not save. Code is posted below. Could someone please take a look and let me know if they have any suggestions? Thanks!
Private Sub Command61_Click()
Dim strFirstName As String
Dim strLastName As String
Dim strIndustry As String
Dim strCountry As String
Dim strState As String
Dim strCity As String
Dim strCompany As String
Dim strTitle As String
Dim strStatus As String
Dim strPhone As String
Dim strEmail As String
Dim strOwner As String
Dim DateNow As String
'Allow user to leave some fields blank. User must fill in certain fields.
Dim VisEnable
If IsNull(Me.txtFirstName) Then
MsgBox ("Please add First Name for this Prospect")
Me.txtFirstName.SetFocus
Exit Sub
End If
If IsNull(Me.txtLastName) Then
MsgBox ("Please add Last Name for this Prospect")
Me.txtLastName.SetFocus
Exit Sub
End If
If IsNull(Me.cboIndustry) Then
Me.cboIndustry = ""
End If
If IsNull(Me.cboGeo) Then
Me.cboGeo = ""
End If
If IsNull(Me.cboInfluence) Then
Me.cboInfluence = ""
End If
If IsNull(Me.cboSchool) Then
Me.cboSchool = ""
End If
If IsNull(Me.cboTier) Then
Me.cboTier = ""
End If
If IsNull(Me.cboCompany) Then
Me.cboCompany = ""
End If
If IsNull(Me.txtTitle) Then
Me.txtTitle = ""
End If
If IsNull(Me.cboStatus) Then
Me.cboStatus = ""
End If
If IsNull(Me.cboOwner) Then
Me.cboOwner = ""
End If
If IsNull(Me.txtPhone) Then
Me.txtPhone = ""
End If
If IsNull(Me.txtEmail) Then
MsgBox ("Please add Email for this Prospect")
Me.txtEmail.SetFocus
Exit Sub
End If
If IsNull(Me.txtNotes) Then
Me.txtNotes = ""
End If
If IsNull(Me.txtInitialProspectEmailSentDate) Then
Me.txtInitialProspectEmailSentDate = ""
End If
If IsNull(Me.txtNextTouchPoint) Then
Me.txtNextTouchPoint = ""
End If
strFirstName = Me.txtFirstName
strLastName = Me.txtLastName
strIndustry = Me.cboIndustry
strCompany = Me.cboCompany
strTitle = Me.txtTitle
strStatus = Me.cboStatus
strPhone = Me.txtPhone
strEmail = Me.txtEmail
strNotes = Me.txtNotes
strOwner = Me.cboOwner
dtEmailSent = Me.txtInitialProspectEmailSentDate
dtNextTouchPoint = Me.txtNextTouchPoint
strRegion = Me.cboGeo
strSoR = Me.cboTier
strInfluence = Me.cboInfluence
strClient = Me.ckClient
strCoworker = Me.ckCoworker
strSchool = Me.cboSchool
strSQL = "Update tblProspect Set FirstName = " & """" & strFirstName & """" & ",LastName = " & """" & strLastName & """" & ",Industry = " & """" & strIndustry & """" & "" & _
",Geography = " & """" & strRegion & """" & ",StrengthofRelationship = " & """" & strSoR & """" & ",School = " & """" & strSchool & """" & ",Company = " & """" & strCompany & """" & "" & _
",Title = " & """" & strTitle & """" & ",Status = " & """" & strStatus & """" & ", InfluenceLevel = " & """" & strInfluence & """" & ", FormerClient = " & strClient & ", FormerCoWorker = " & strCoworker & "" & _
",Email = " & """" & strEmail & """" & ",Phone = " & """" & strPhone & """" & ",ProspectOwner = " & """" & strOwner & """" & ",Notes = " & """" & strNotes & """" & ""
If dtNextTouchPoint <> "" Then
strSQL = strSQL & " ,NextTouchPoint = #" & dtNextTouchPoint & "#"
End If
If dtEmailSent <> "" Then
strSQL = strSQL & " ,LastEmailDate = #" & dtEmailSent & "#"
End If
strSQL = strSQL & " WHERE Email = " & """" & strEmail & """" & ""
DoCmd.SetWarnings False
DoCmd.RunSQL strSQL
DoCmd.SetWarnings True
Dim ctl As Control
For Each ctl In Me.Controls
Select Case ctl.ControlType
Case acTextBox, acComboBox, acListBox, acCheckBox
If ctl.ControlSource = "" Then
ctl.Value = Null
End If
Case Else
End Select
Next ctl
Me.Visible = False
DoCmd.OpenForm "frmProspectAdmin", acNormal, , , acFormEdit, acWindowNormal
DoCmd.RunCommand acCmdSaveRecord
Form_frmProspectProfile.Refresh
Form_frmProspectAdmin.Refresh
End Sub
It comes out exactly likely I want it to in Debug.Print, but it does not save that way in the contact profile.
Debug.Print strSQL
Update tblProspect Set FirstName = "Jon",LastName = "Snow",Industry = "Other",Geography = "",StrengthofRelationship = "",School = "",Company = "",Title = "",Status = "Dead", InfluenceLevel = "", FormerClient = 0, FormerCoWorker = 0,Email = "jsnow#winterfell",Phone = "",ProspectOwner = "",Notes = ""
Related
I am facing a little issue now. My issue is that currently when i made edit to the current record in my subform and click on "update" it will overwrite my previous record which i do not want.
Instead, I would like to add my edited data record from the subform and insert it into the subform as a new record with the same PO number as the one that i am editing.
Below are my code:
Private Sub cmdAdd_Click()
'when we click on button Add there are two options
'1. for insert
'2. for update
If Me.txtID.Tag & "" = "" Then
'this is for insert new
'add data to table
CurrentDb.Execute "INSERT INTO mxd " & _
"(ID,Fabrication,Width,FinishedGoods,Colour, " & _
"LabDipCode,GrossWeight,NettWeight, " & _
"Lbs,Loss,Yds,Remarks,POType,ComboName,GroundColour)" & _
" VALUES(" & Me.txtID & ",'" & Me.txtFabrication & "','" & _
Me.txtWidth & "','" & Me.txtFinishedGood & "','" & _
Me.txtColour & "','" & Me.txtLabDipCode & "','" & _
Me.txtGrossweight & "','" & _ Me.txtNettweight & "','" & _
Me.txtLbs & "','" & Me.txtLoss & "','" & _ Me.txtYds & "','" & _
Me.txtRemarks & "','" & Me.cboPoType "','" & _
Me.txtGroundColour & "','" & Me.txtComboName & "')"
Else
'otherwise (Tag of txtID store the id of student to be modified)
CurrentDb.Execute "UPDATE mxd " & _
" SET ID = " & Me.txtID & _
", Fabrication = '" & Me.txtFabrication & "'" & _
", Width = '" & Me.txtWidth & "'" & _
", FinishedGoods = '" & Me.txtFinishedGood & "'" & _
", Colour = '" & Me.txtColour & "'" & _
", LabDipCode = '" & Me.txtLabDipCode & "'" & _
", GrossWeight = '" & Me.txtGrossweight & "'" & _
", NettWeight = '" & Me.txtNettweight & "'" & _
", LBS = '" & Me.txtLbs & "'" & _
", Loss = '" & Me.txtLoss & "'" & _
", Yds = '" & Me.txtYds & "'" & _
", Remarks = '" & Me.txtRemarks & "'" & _
", POType = '" & Me.cboPoType & "'" & _
", ComboName = '" & Me.txtComboName & "'" & _
", GroundColour = '" & Me.txtGroundColour & "'" & _
" WHERE ID = " & Me.txtID.Tag
End If
'clear form
cmdClear_Click
'refresh data in list on form
FormMxdSub.Form.Requery
End Sub
Private Sub cmdClear_Click()
Me.txtID = ""
Me.txtFabrication = ""
Me.txtWidth = ""
Me.txtFinishedGood = ""
Me.txtColour = ""
Me.txtLabDipCode = ""
Me.txtGrossweight = ""
Me.txtNettweight = ""
Me.txtLbs = ""
Me.txtLoss = ""
Me.txtYds = ""
Me.txtRemarks = ""
Me.cboPoType = ""
Me.txtKeywords = ""
Me.txtComboName = ""
Me.txtGroundColour = ""
'focus on ID text box
Me.txtID.SetFocus
'set button edit to enable
Me.cmdEdit.Enabled = True
'change caption of button add to Add
Me.cmdAdd.Caption = "Add"
'clear tag on txtID for reset new
Me.txtID.Tag = ""
End Sub
Private Sub cmdClose_Click()
DoCmd.Close
End Sub
Private Sub cmdDelete_Click()
'delete record
'check existing selected record
If Not (Me.FormMxdSub.Form.Recordset.EOF And
Me.FormMxdSub.Form.Recordset.BOF) Then
'confirm delete
If MsgBox("Are you sure you want to delete?", vbYesNo) = vbYes Then
'delete now
CurrentDb.Execute "DELETE FROM mxd " & _
"where ID = " & Me.FormMxdSub.Form.Recordset.Fields("ID")
'refresh data in list
Me.FormMxdSub.Form.Requery
End If
End If
End Sub
Private Sub cmdEdit_Click()
'check whether there is exists data in list
If Not (Me.FormMxdSub.Form.Recordset.EOF And
Me.FormMxdSub.Form.Recordset.BOF) Then
'get data to text box control
With Me.FormMxdSub.Form.Recordset
Me.txtID = .Fields("ID")
Me.txtFabrication = .Fields("Fabrication")
Me.txtWidth = .Fields("Width")
Me.txtFinishedGood = .Fields("FinishedGoods")
Me.txtColour = .Fields("Colour")
Me.txtLabDipCode = .Fields("LabDipCode")
Me.txtGrossweight = .Fields("GrossWeight")
Me.txtNettweight = .Fields("NettWeight")
Me.txtLbs = .Fields("Lbs")
Me.txtLoss = .Fields("Loss")
Me.txtYds = .Fields("Yds")
Me.txtRemarks = .Fields("Remarks")
Me.cboPoType = .Fields("POType")
Me.txtComboName = .Fields("ComboName")
Me.txtGroundColour = .Fields("GroundColour")
'store id of student in Tag of txtID in case id is modified
Me.txtID.Tag = .Fields("ID")
'change caption of button add to update
'Me.cmdAdd.Caption = "Update"
'disable button edit
Me.cmdEdit.Enabled = False
End With
End If
End Sub
You can use the RecordsetClone to create a dupe of the current record. Much faster and cleaner and no Tag is needed:
Private Sub btnCopy_Click()
Dim rstSource As DAO.Recordset
Dim rstInsert As DAO.Recordset
Dim fld As DAO.Field
If Me.NewRecord = True Then Exit Sub
Set rstInsert = Me.RecordsetClone
Set rstSource = rstInsert.Clone
With rstSource
If .RecordCount > 0 Then
' Go to the current record.
.Bookmark = Me.Bookmark
With rstInsert
.AddNew
For Each fld In rstSource.Fields
With fld
If .Attributes And dbAutoIncrField Then
' Skip Autonumber or GUID field.
ElseIf .Name = "SomeFieldToExclude" Then
' Leave field blank.
ElseIf .Name = "SomeOtherFieldToExclude" Then
' Leave field blank.
Else
' Copy field content.
rstInsert.Fields(.Name).Value = .Value
End If
End With
Next
.Update
' Go to the new record and sync form.
.MoveLast
Me.Bookmark = .Bookmark
.Close
End With
End If
.Close
End With
Set rstInsert = Nothing
Set rstSource = Nothing
End Sub
Of course, if you place the button on the main form, replace in the code Me with a reference to the subform: Me!NameOfYourSubformControl.Form
I'm writing VBA code for an application in Access for the first time and have created two separate strings to filter a report. The first of these strFilter filters the reports based on criteria in a list box. The second strWhere has been set up to filter the report based on a date input into a pair of text boxes. Both of these string filters work perfectly when used separately.
What I want to know is if there is a way to combine the two strings easily so that the user an filter the report based on both the criteria in the list box and the date they have entered in the text boxes.
The code I have for the listbox filter when it is added to the reports filter currently looks like this:
With Reports![rptFaultRecords]
.Filter = strFilter
.FilterOn = True
I want to add the string to filter by date strWhere next to the strFilter so the report can be filtered by both date and list criteria. All the code I've entered when attempting to do this has given me a run time error 3075. Is it possible for these two strings to be combined easily and if so how can I do it?
The rest of the code I've written is below if you need to see it:
Private Sub btnAllFaultsFilter_Click()
Dim varItem As Variant
Dim strRoom As String
Dim strFilter As String
Dim strDevice As String
Dim strCat As String
Dim strStatus As String
Dim strDateField As String
Dim strWhere As String
Const strcJetDate = "\#mm\/dd\/yyyy\#"
strDateField = "[f_datereported]"
If IsDate(Me.txtStartDate) Then
strWhere = "(" & strDateField & " >= " & Format(Me.txtStartDate, strcJetDate) & ")"
End If
If IsDate(Me.txtEndDate) Then
If strWhere <> vbNullString Then
strWhere = strWhere & " AND "
End If
strWhere = strWhere & "(" & strDateField & " < " & Format(Me.txtEndDate + 1, strcJetDate) & ")"
End If
For Each varItem In Me.lstRoom.ItemsSelected
strRoom = strRoom & ",'" & Me.lstRoom.ItemData(varItem) & "'"
Next varItem
If Len(strRoom) = 0 Then
strRoom = "Like '*'"
Else
strRoom = Right(strRoom, Len(strRoom) - 1)
strRoom = "IN(" & strRoom & ")"
End If
For Each varItem In Me.lstDevice.ItemsSelected
strDevice = strDevice & ",'" & Me.lstDevice.ItemData(varItem) & "'"
Next varItem
If Len(strDevice) = 0 Then
strDevice = "Like '*'"
Else
strDevice = Right(strDevice, Len(strDevice) - 1)
strDevice = "IN(" & strDevice & ")"
End If
For Each varItem In Me.lstCategory.ItemsSelected
strCat = strCat & ",'" & Me.lstCategory.ItemData(varItem) & "'"
Next varItem
If Len(strCat) = 0 Then
strCat = "Like '*'"
Else
strCat = Right(strCat, Len(strCat) - 1)
strCat = "IN(" & strCat & ")"
End If
For Each varItem In Me.lstStatus.ItemsSelected
strStatus = strStatus & ",'" & Me.lstStatus.ItemData(varItem) & "'"
Next varItem
If Len(strStatus) = 0 Then
strStatus = "Like '*'"
Else
strStatus = Right(strStatus, Len(strStatus) - 1)
strStatus = "IN(" & strStatus & ")"
End If
strFilter = "[c_roomid] " & strRoom & " AND [f_computername] " & strDevice & " AND [f_faultcategory] " & strCat & " AND [f_faultstatus] " & strStatus
With Reports![rptFaultRecords]
'.Filter = strFilter
.Filter = strFilter
.FilterOn = True
End With
End Sub
I have a list of names in a subform, and on my main form I have a button that allows the user to view the "profile" of a given contact. Once in a profile, I would like there to be a button that allows the user to move to the next name in the subform (while staying the "profile" view) by clicking "next user".
In addition, the DB asks the user whether she/he wants to save changes (vbYesNo) to the profile before moving to the next user's profile. For some reason, my code works the when the user clicks "next contact" and "yes" the first time, but it will not scroll to the next contact each subsequent time the user clicks "next contact" and "yes". Note that the "next user" button works fine if the user selects "no" for when she/he does not want to save changes made to the profile.
Here is the code:
Private Sub Command65_Click()
Dim strFirstName As String
Dim strLastName As String
Dim strIndustry As String
Dim strCountry As String
Dim strState As String
Dim strCity As String
Dim strCompany As String
Dim strTitle As String
Dim strStatus As String
Dim strPhone As String
Dim strEmail As String
Dim strOwner As String
Dim DateNow As String
Dim rs As DAO.Recordset
'Allow user to leave some fields blank. User must fill in certain fields.
Dim VisEnable
intMsg = MsgBox("Would you like to save the current contact's information?", vbYesNo)
If intMsg = 6 Then
If IsNull(Me.txtFirstName) Then
MsgBox ("Please add First Name for this Prospect")
Me.txtFirstName.SetFocus
Exit Sub
End If
If IsNull(Me.txtLastName) Then
MsgBox ("Please add Last Name for this Prospect")
Me.txtLastName.SetFocus
Exit Sub
End If
If IsNull(Me.cboIndustry) Then
Me.cboIndustry = ""
Exit Sub
End If
If IsNull(Me.cboGeo) Then
Me.cboGeo = ""
End If
If IsNull(Me.cboInfluence) Then
Me.cboInfluence = ""
End If
If IsNull(Me.cboSchool) Then
Me.cboSchool = ""
End If
If IsNull(Me.cboTier) Then
Me.cboTier = ""
End If
If IsNull(Me.cboCompany) Then
Me.cboCompany = ""
End If
If IsNull(Me.txtTitle) Then
Me.txtTitle = ""
End If
If IsNull(Me.cboStatus) Then
Me.cboStatus = ""
Exit Sub
End If
If IsNull(Me.cboOwner) Then
Me.cboOwner = ""
End If
If IsNull(Me.txtPhone) Then
Me.txtPhone = ""
End If
If IsNull(Me.txtEmail) Then
MsgBox ("Please add Email for this Prospect")
Me.txtEmail.SetFocus
Exit Sub
End If
If IsNull(Me.txtNotes) Then
Me.txtNotes = ""
Exit Sub
End If
If IsNull(Me.txtInitialProspectEmailSentDate) Then
Me.txtInitialProspectEmailSentDate = ""
End If
If IsNull(Me.txtNextTouchPoint) Then
Me.txtNextTouchPoint = ""
End If
strFirstName = Me.txtFirstName
strLastName = Me.txtLastName
strIndustry = Me.cboIndustry
strCompany = Me.cboCompany
strTitle = Me.txtTitle
strStatus = Me.cboStatus
strPhone = Me.txtPhone
strEmail = Me.txtEmail
strNotes = Me.txtNotes
strOwner = Me.cboOwner
dtEmailSent = Me.txtInitialProspectEmailSentDate
dtNextTouchPoint = Me.txtNextTouchPoint
strRegion = Me.cboGeo
strSoR = Me.cboTier
strInfluence = Me.cboInfluence
strClient = Me.ckClient
strCoworker = Me.ckCoworker
strSchool = Me.cboSchool
strSQL = "Update tblProspect Set FirstName = " & """" & strFirstName & """" & ",LastName = " & """" & strLastName & """" & ",Industry = " & """" & strIndustry & """" & "" & _
",Geography = " & """" & strRegion & """" & ",StrengthofRelationship = " & """" & strSoR & """" & ",School = " & """" & strSchool & """" & ",Company = " & """" & strCompany & """" & "" & _
",Title = " & """" & strTitle & """" & ",Status = " & """" & strStatus & """" & ", InfluenceLevel = " & """" & strInfluence & """" & ", FormerClient = " & strClient & ", FormerCoWorker = " & strCoworker & "" & _
",Email = " & """" & strEmail & """" & ",Phone = " & """" & strPhone & """" & ",ProspectOwner = " & """" & strOwner & """" & ",Notes = " & """" & strNotes & """" & ""
If dtNextTouchPoint <> "" Then
strSQL = strSQL & " ,NextTouchPoint = #" & dtNextTouchPoint & "#"
End If
If dtEmailSent <> "" Then
strSQL = strSQL & " ,LastEmailDate = #" & dtEmailSent & "#"
End If
strSQL = strSQL & " WHERE Email = " & """" & strEmail & """" & ""
DoCmd.SetWarnings False
DoCmd.RunSQL strSQL
DoCmd.SetWarnings True
intRecord = Me.txtRecord + 1
Set rs = CurrentDb.OpenRecordset("qselProspects")
If rs.RecordCount <> 0 Then
rs.MoveLast
If intRecord = 1 Then
intRecord = rs.RecordCount + 1
End If
End If
If rs.RecordCount <> 0 Then
rs.MoveFirst 'Unnecessary in this case, but still a good habit
Do Until rs.EOF = True
If intRecord = rs.AbsolutePosition Then
Me.txtRecord = intRecord
Me.txtFirstName = rs!FirstName
Me.txtLastName = rs!LastName
Me.txtTitle = rs!Title
Me.cboCompany = rs!Company
Me.cboIndustry = rs!Industry
Me.cboGeo = rs!Geography
Me.cboTier = rs!StrengthofRelationship
Me.cboIndustry = rs!InfluenceLevel
Me.cboSchool = rs!School
Me.ckClient = rs!FormerClient
Me.ckCoworker = rs!FormerCoWorker
Me.cboStatus = rs!Status
Me.cboOwner = rs!ProspectOwner
Me.txtEmail = rs!Email
Me.txtPhone = rs!Phone
Me.txtNextTouchPoint = rs!NextTouchPoint
Me.txtNotes = rs!Notes
Me.txtInitialProspectEmailSentDate = rs!LastEmailDate
End If
rs.MoveNext
Loop
End If
'''///If you choose No it works, but if you choose Yes it does not...very strange
Else
intRecord = Me.txtRecord + 1
Set rs = CurrentDb.OpenRecordset("qselProspects")
If rs.RecordCount <> 0 Then
rs.MoveLast
If rs.RecordCount = intRecord Then
intRecord = 0
End If
End If
If rs.RecordCount <> 0 Then
rs.MoveFirst
Do Until rs.EOF = True
If intRecord = rs.AbsolutePosition Then
Me.txtRecord = intRecord
Me.txtFirstName = rs!FirstName
Me.txtLastName = rs!LastName
Me.txtTitle = rs!Title
Me.cboCompany = rs!Company
Me.cboIndustry = rs!Industry
Me.cboGeo = rs!Geography
Me.cboTier = rs!StrengthofRelationship
Me.cboIndustry = rs!InfluenceLevel
Me.cboSchool = rs!School
Me.ckClient = rs!FormerClient
Me.ckCoworker = rs!FormerCoWorker
Me.cboStatus = rs!Status
Me.cboOwner = rs!ProspectOwner
Me.txtEmail = rs!Email
Me.txtPhone = rs!Phone
Me.txtNextTouchPoint = rs!NextTouchPoint
Me.txtNotes = rs!Notes
Me.txtInitialProspectEmailSentDate = rs!LastEmailDate
End If
rs.MoveNext
Loop
End If
End If
End Sub
Thanks to whoever can figure this out! This has eaten up too many hours as it is.
This is not an answer, but I write it here because it does not fit in a comment. A few advises that if you have applied, would have spared you all this head-ache.
1) your code follows the pattern
If User_Says_Yes Then
Save
Fetch_Next_Record
Else
Fetch_Next_Record
Endif
This is problematic because the Fetch_Next_Record is a lot of code and it is duplicated, and you spend a lot of time to see where it differs. duplicating code is generally a very bad idea. Try to rewrite it with the following pattern:
If User_Says_Yes Then
Save
Endif
Fetch_Next_Record
2) Try to make your code shorter, by moving as much as you can to private subroutines. for example, write some Function like BuildSQL() as String, a subroutine like updateFormFromRs(rs as Recordset). In General, when any of your routines or functions get too long, say more than 20 or 30 lines, you should think of migrating some code to subroutines and functions
3) Indent your code. It is so difficult to follow your code without it.. just to see where was the Else that starts when the user says no...
4) You fetch a whole table in the recordset, just to scroll it and find one record to display that matches if intRecord = rs.AbsolutePosition? Why not use a SQL statement with a WHERE clause and load just the desired record? This is something you need to apply in any serious application with a decent amount of data.
5) statements like If rs.EOF = True Then: Simply If rs.EOF Then.
The additional = True will not make the test more strict whatsoever. as if without it we check if the condition was almost true.
Finally, even if you have possibly inherited this code from someone else, I am sure that you will have to rewrite it completely and improve it, the sooner the better. And yes, I am sure that if you follow these guidelines, you will be able to debug you code very easily.
Friendly :)
I am trying to implement millisecond timestamping in Access 2010/13 using this method;
MS Access Can Handle Millisecond Time Values--Really - See more at:
The Millisecond value is queried by;
SELECT DateValueMsec([DateTimeMs]) AS DateOnly FROM - to provide a date only control to sort the form from a textbox.
Any filter applied programmatically on DateOnly yeilds 0 results.
Private Sub BuildFilter()
Dim strFilter As String
Dim ctl As Control
strFilter = ""
'add selected values to string
For Each ctl In Me.FormHeader.Controls
With ctl
If .ControlType = acTextBox Or .ControlType = acComboBox Then
If Nz(.Value) <> "" Then
If InStr(.Name, "Date") <> 0 Then
If Nz(StartDate) <> "" And Nz(EndDate) <> "" And InStr(strFilter, "DateOnly") = 0 Then
strFilter = strFilter & "[DateOnly] BETWEEN #" & Me.StartDate.Value & "# AND #" & Me.EndDate.Value & "# AND "
ElseIf Nz(StartDate) <> "" And InStr(strFilter, "DateOnly") = 0 Then
strFilter = strFilter & "[DateOnly] >= #" & DateValueMsec(Me.StartDate.Value) & "# AND "
' strFilter = strFilter & "[DateOnly] >= #" & Me.StartDate.Value & "# AND "
ElseIf Nz(EndDate) <> "" And InStr(strFilter, "DateOnly") = 0 Then
strFilter = strFilter & "[DateOnly] <= #" & Me.EndDate.Value & "# AND "
End If
ElseIf InStr(.Name, "ID") <> 0 Then
strFilter = strFilter & "[" & .Name & "] = " & .Value & " AND "
Else
strFilter = strFilter & "[" & .Name & "] = '" & .Value & "' AND "
End If
End If
End If
End With
Next ctl
'trim trailing
strFilter = TrimR(strFilter, 5)
Debug.Print strFilter
With Me.subfrmzzAuditTrailDisplay
.Form.Filter = strFilter
.Form.FilterOn = True
End With
End Sub
Answer! From #pathDongle
Time is stored as Millisecond UTC;
!DateTimeMS = GetTimeUTC()
And restored by;
Public Function UTCtoTimeLocal(dSysUTC As Date) As Date
'Dim sysTime As SYSTEMTIME
Dim DST As Long
Dim tzi As TIME_ZONE_INFORMATION
DST = GetTimeZoneInformation(tzi)
UTCtoTimeLocal = dSysUTC - TimeSerial(0, tzi.Bias, 0) + IIf(DST = 2, TimeSerial(1, 0, 0), 0)
End Function
Query;
SELECT tblzzAuditTrail.DateTimeMS, FormatDate(UTCtoTimeLocal([DateTimeMS])) AS DateTimeLocal
Which can be filtered on as a String.
Private Sub BuildFilter()
Dim strFilter As String
Dim ctl As Control
strFilter = ""
'add selected values to string
For Each ctl In Me.FormHeader.Controls
With ctl
If .ControlType = acTextBox Or .ControlType = acComboBox Then
If Nz(.Value) <> "" Then
If InStr(.Name, "Date") <> 0 Then
If Nz(StartDate) <> "" And Nz(EndDate) <> "" And InStr(strFilter, "DateTimeLocal") = 0 Then
strFilter = strFilter & "[DateTimeLocal] BETWEEN '" & FormatDate(Me.StartDate.Value) & "' AND '" & FormatDate(Me.EndDate.Value) & "' AND "
ElseIf Nz(StartDate) <> "" And InStr(strFilter, "DateTimeLocal") = 0 Then
strFilter = strFilter & "[DateTimeLocal] > '" & FormatDate(Me.StartDate.Value) & "' AND "
ElseIf Nz(EndDate) <> "" And InStr(strFilter, "DateTimeLocal") = 0 Then
strFilter = strFilter & "[DateTimeLocal] <= '" & FormatDate(Me.EndDate.Value) & "' AND "
End If
ElseIf InStr(.Name, "ID") <> 0 Then
strFilter = strFilter & "[" & .Name & "] = " & .Value & " AND "
Else
strFilter = strFilter & "[" & .Name & "] = '" & .Value & "' AND "
End If
End If
End If
End With
Next ctl
'trim trailing And
strFilter = TrimR(strFilter, 5)
Debug.Print strFilter
With Me.subfrmzzAuditTrailDisplay
.Form.Filter = strFilter
.Form.FilterOn = True
End With
End Sub
Resulting Filter String;
[UserID] = 2 AND [DateTimeLocal] BETWEEN '06/01/2015 00:00:00.000' AND '07/01/2015 00:00:00.000'
As per my other question;
millisecond-time-msec2-incorrect-return
The strFilter results in the following, but will not carry through to the Me.Filter, any ideas why?
strFilter = "'20410' OR 'A20000' OR 'A20400'"
Private Sub Image_OffsetFilterButton_Click()
Dim strFilter As String
strFilter = "'A20410'"
'/ see if there is data in Field Box Text907, if so add it to the filter
If Me!Text907 & vbNullStr <> vbNullStr Then
strFilter = strFilter & " OR " & "'" & "A20000" & "'"
End If
If Me!Text910 & vbNullStr <> vbNullStr Then
' If FieldB is of Text type, use the ' delimiter
strFilter = strFilter & " OR " & "'" & "A20024" & "'"
End If
If Me!Text911 & vbNullStr <> vbNullStr Then
' If FieldB is of Text type, use the ' delimiter
strFilter = strFilter & " OR " & "'" & "A20400" & "'"
End If
'/<etc>
If strFilter <> "" Then
' trim off leading "OR"
Me.Filter = "[Account] = strFilter AND [BU] = 'B50931'"
Me.FilterOn = True
Else
Me.Filter = ""
Me.FilterOn = False
End If
End Sub
Private Sub Image_OffsetFilterButton_Click()
Dim strFilter As String
strFilter = ""
'/ see if there is data in Field Box Text907, if so add it to the filter
If Me!Text907 & vbNullStr <> vbNullStr Then
strFilter = strFilter & " OR [ACCOUNT] = '" & Me.Text907 & "'"
End If
If Me!Text910 & vbNullStr <> vbNullStr Then
'/ If FieldB is of Text type, use the ' delimiter
strFilter = strFilter & " OR [ACCOUNT] = '" & Me.Text910 & "'"
End If
If Me!Text911 & vbNullStr <> vbNullStr Then
'/ If FieldB is of Text type, use the ' delimiter
strFilter = strFilter & " OR [ACCOUNT] = '" & Me.Text911 & "'"
End If
'/<etc>
If strFilter <> "" Then
'\ trim off leading "OR"
Me.Filter = Mid(strFilter, 4)
Me.FilterOn = True
Else
Me.Filter = "[BU] = [Forms]![Frm_Main]! [Frm_Main_TextBox_Display_BU_Number_HIDDEN].Value"
Me.FilterOn = False
End If
End Sub