I'm designing a task order db that has a lot of checkboxes and instead of creating Yes/No fields it was decided to use Date fields that use Checkboxes on the Forms to set said dates using the = Now() function:
Private Sub DXF_Chk_Click()
dxfLimits = Now()
End Sub
However, if someone unchecks the checkbox the date just updates and in our Reports the boxes remain checked.
My thought was to write a code that checks to see if the Date field has been filled and Null it On Click but that's not working:
If Me.DXF_Chk = 1 Then
Me.dxfLimits = Now()
Else
Me.dxfLimits = Null
End If
Or leave the current On Click alone and use some code in the After_Update event. I'm not sure which is proper protocol, nor what I'm doing wrong in the code itself.
Sorry, I'm a noob.
This is what I would do:
Me.dxfLimits = IIF(Me.DXF_chk, Now(), NULL)
Try this
Private Sub DXF_chk_AfterUpdate()
If Me.DXF_chk.Value Then
Me.dxfLimits = Now()
Else
Me.dxfLimits = Null
End If
End Sub
OR
Private Sub DXF_chk_AfterUpdate()
If Me.DXF_chk = -1 Then
Me.dxfLimits = Now()
Else
Me.dxfLimits = Null
End If
End Sub
Note: -1 = true/yes, 0=false/no
Related
I am trying to create a simple form for entering data. I have two tables, jobs and reports. The report table refers to a job with a one to many (one job, many reports). When browsing through the reports I want the combo box that lists all of the jobs to show the corresponding job as the selected value. This is easy in a .NET environment, but I'm not understanding how to set this up in the property sheet for the combobox. My ComboBox record source is from a query:
SELECT Jobs.UID, Jobs.Projectcode, Jobs.Projectname, Jobs.Owner, Jobs.Contractor
FROM Jobs
ORDER BY Jobs.[Projectcode];
And the form is based on a query that joins the tables:
SELECT Report.ID, Report.ReportNumber, Report.ReportDate, Report.Temperature, Report.Weather, Report.Progress, Report.PeopleatOAC, Report.Trades, Jobs.UID, Jobs.Projectname, Jobs.Owner, Jobs.Contractor
FROM Report
INNER JOIN Jobs ON Jobs.UID = Report.JobID
UNION ALL SELECT Report.ID, Report.ReportNumber, Report.ReportDate,
Report.Temperature, Report.Weather, Report.Progress, Report.PeopleatOAC,
Report.Trades, Jobs.UID, Jobs.Projectname, Jobs.Owner, Jobs.Contractor
FROM Report
LEFT JOIN Jobs ON Jobs.UID = Report.JobID WHERE (((Report.JobID) Is Null))
ORDER BY Report.ID;
The way I have this set up, a report can have a null job field. So I want to be able to select a job from the combo box to update the report table AND I want the combo box to reflect the correct job if the current record has a jobID associated with it. Is this possible?
What I have implemented is a tie to the Current event for the form that sets the combobox value (or clears it) depending on the JobID. As well as an tied to the changed event for the combobox to update the database with a selection. This works well enough but VBA feels so limiting compared to C#, WPF, and MVVM.
For anyone stumbling accross this with a similar question here are the 2 VBA functions:
Private Sub Form_Current()
Dim JobID As Integer
Dim i As Integer
Dim TempVal As Variant
Dim TestVal As Integer
TempVal = Me.JobID.Value
If Me.JobID.Value <> Empty Then
JobID = Me.JobID.Value
Else: JobID = -2
End If
With Me.JobCodeCombo
If (JobID >= 0) Then
For i = 0 To .ListCount - 1
TestVal = .Column(0, i)
If .Column(0, i) = JobID Then
.Value = .ItemData(i)
Exit For
End If
Next
Else
Me.JobCodeCombo = Null
End If
End With
End Sub
Private Sub JobCodeCombo_Change()
Dim ReportID As Long
Dim JobID As Long
Dim dbs As DAO.Database
Dim qdfUpdateJobID As DAO.QueryDef
Dim CurrentRecord As Long
CurrentRecord = Me.CurrentRecord
Set dbs = CurrentDb
Set qdfUpdateJobID = dbs.QueryDefs("UpdateReportWithJobID")
ReportID = Me.ReportID.Value
JobID = Me.JobCodeCombo.Column(0)
qdfUpdateJobID.Parameters(0).Value = JobID
qdfUpdateJobID.Parameters(1).Value = ReportID
qdfUpdateJobID.Execute
qdfUpdateJobID.Close
DoCmd.Save acForm, "Form1"
Me.Requery
DoCmd.GoToRecord acDataForm, "Form1", acGoTo, CurrentRecord
End Sub
The query called from the second function is a simple update query in my access file that has two parameters:
PARAMETERS [P1] Long, [P2] Long;
UPDATE Report SET JobID = P1
WHERE [ID] = P2;
So I'm relatively new to MS Access but have developed a relatively comprehensive CRM tool for my organization that allows users to record company employees, organization details, and meeting details.
I've run into an odd problem with my "Edit Organization" form. When I use the unbound lookup combobox to change records, it loads up the new record correctly; however, the record number showing at the bottom of the page appears to be random... for example if I select the 6th record using the lookup box, it might show as record number 96 (or something else totally random).
I'm not super concerned by this (although somewhat); however, one of the other odd behaviours is that the lookup works for all records except when changing back to the first organization in the database. In this case, it doesn't load the EmployeeID and related information properly (but this is fine for all other records in the database).
Any suggestions would be great. The code the page is as below. I'm sure there is some extraneous code in there at the moment, but none of it should affect this I don't think...
'Updates the Phone and Email text boxes when the Employee is changed in the dropdown menu
Private Sub cboEmployee_Change()
Me.txtPhone.Value = Me.cboEmployee.Column(2)
Me.txtEmail.Value = Me.cboEmployee.Column(3)
Me.cboRelationship.Value = Null
Me.cboBranch.Requery
Me.cboMinistry.Requery
Me.cboDivision.Requery
End Sub
'Updates the Phone and Email text boxes when the Employee is changed in the dropdown menu
Private Sub cboEmployee_AfterUpdate()
Me.txtPhone.Value = Me.cboEmployee.Column(2)
Me.txtEmail.Value = Me.cboEmployee.Column(3)
Me.cboRelationship.Value = Null
Me.cboBranch.Requery
Me.cboMinistry.Requery
Me.cboDivision.Requery
End Sub
'Requeries the various other boxes on the form after a Ministry is selected and clears the Phone and Email fields. Bit of a hack to use Employee 43 (blank vs. null)
Private Sub cboMinistry_Change()
Me.cboDivision.Value = Null
Me.cboBranch.Value = Null
Me.cboDivision.Requery
Me.cboBranch.Requery
Me.cboEmployee.Requery
Me.txtPhone = Null
Me.txtEmail = Null
End Sub
'Requeries the various other boxes on the form after a Division is selected and clears the Phone and Email fields. Bit of a hack to use Employee 43 (blank vs. null)
Private Sub cboDivision_Change()
Me.cboMinistry.Requery
Me.cboBranch.Requery
Me.cboEmployee.Requery
Me.txtPhone = Null
Me.txtEmail = Null
Me.cboRelationship.Value = Null
End Sub
'Requeries the various other boxes on the form after a Branch is selected and clears the Phone and Email fields. Bit of a hack to use Employee 43 (blank vs. null)
Private Sub cboBranch_Change()
Me.cboDivision.Requery
Me.cboMinistry.Requery
Me.cboEmployee.Requery
Me.txtPhone = Null
Me.txtEmail = Null
Me.cboRelationship.Value = Null
End Sub
'Populates the form with the appropriate details if there is an EmployeeID associated with an organization
Private Sub Form_Current()
Me.cboLookup.Value = Organization
If IsNull(EmployeeID) = True Then
Me.cboMinistry.Value = Null
Me.cboDivision.Value = Null
Me.cboBranch.Value = Null
Me.txtPhone.Value = Null
Me.txtEmail.Value = Null
Else
Me.cboEmployee.Value = EmployeeID
Me.cboBranch.Value = BranchID
Me.cboDivision.Value = DLookup("DivisionID", "tblBranch", "BranchID=" & BranchID)
Me.cboMinistry.Value = DLookup("MinistryID", "tblDivision", "DivisionID=" & Me.cboDivision)
Me.cboEmployee.Requery
Me.cboBranch.Requery
Me.cboDivision.Requery
Me.txtPhone.Value = PhoneNumber
Me.txtEmail.Value = Email
Me.Dirty = False
End If
End Sub
Good morning All,
I am working on the following however, I can't figure it out for the life of me. I have table that contains a bunch of due dates. What I am trying to do is add a query that adds another field and inserts a yes if the date equals a day in last week. Honestly I need it to insert yes as long as it does not = this week.
I've tried using:
Urgent: IIf([Due Date]=[Due Date]
Between
DateAdd("d",1-Weekday(Date()) 7, Date()) And
DateAdd("d",1-Weekday(Date())-1, Date()),"Yes","")
with no luck. What am I missing here?!
Thanks all. Cheers.
Here's how to return Urgent if 'not this week' means previous Saturday and before. Not sure how you would do it without code.
Helper Function to get the first day of this week based on current date. Paste it into a new or existing global module.
Function FirstDateOfTheWeek() As Date
Dim dt As Date
If Weekday(Date) = vbSaturday Then
FirstDateOfTheWeek = DateAdd("y", -6, Date) 'today is Saturday? then return previous Sunday
ElseIf Weekday(Date) = vbSunday Then
FirstDateOfTheWeek = Date 'today is Sunday? then return Sunday because its the first day of the week
Else 'weekday, so just go backwards until we hit previous sunday's date
dt = Date
While Weekday(dt) <> vbSunday
dt = DateAdd("y", -1, dt)
Wend
FirstDateOfTheWeek = dt
End If
End Function
An IsUrgent function to be called from query. Paste it into a new or existing global module.
Function IsUrgent(dt As Variant) As String
If IsNull(dt) Then 'if null date is passed then return blank string; Variant chosen instead of date for this case; change it to N/A if you want?
IsUrgent = ""
Exit Function
End If
If dt < FirstDateOfTheWeek Then
IsUrgent = "Yes"
Else
IsUrgent = ""
End If
End Function
The Query Column calling IsUrgent() function:
IsUrgent: IsUrgent([Due Date])
I'm trying to write code in Access 2010 that when the [Validate] button is clicked, it will analyze multiple fields (8 in total) for a value (or no value) and then return a statement (or text) in another field ([AppStatus]) based on whether all of the 8 fields are entered or not. In other words, if any of the fields are null, the [AppStatus] field should populate with the default text of "RE PRE-QUAL". Below is where I started but I can't seem to figure out why it is not working.
Private Sub Validate_Click()
If [PrimarySSN].Value = Null Then
If [PropAddress].Value = Null Then
If [PropCity].Value = Null Then
If [PropState].Value = Null Then
If [PropZipCode].Value = Null Then
If [RequestedLoanAmount].Value = Null Then
If [BorrowerIncome.Value] = Null Then
If [EstHomeValue].Value = Null Then
[AppStatus].Value = "RE PRE-QUAL"
ElseIf [PrimarySSN].Value = Not Null Then
ElseIf [PropAddress].Value = Not Null Then
ElseIf [PropCity].Value = Not Null Then
ElseIf [PropState].Value = Not Null Then
ElseIf [PropZipCode].Value = Not Null Then
ElseIf [RequestedLoanAmount].Value = Not Null Then
ElseIf [BorrowerIncome].Value = Not Null Then
ElseIf [EstHomeValue].Value = Not Null Then
[AppStatus].Value = Null
End If
End If
End If
End If
End If
End If
End If
End Sub
One of the reasons it's not working is because Nothing is ever equal to Null, not even another Null. Another issue is you can't use Not Null in VBA code. (Not Null is valid in Access SQL, but that doesn't help here.) In VBA, use IsNull() to check whether a value is Null.
You want "RE PRE-QUAL" as the value of AppStatus whenever one or more of those other 8 fields is Null (ie IsNull(fieldname.Value) = True). Otherwise, AppStatus will be Null. So you could do something like this ...
If IsNull(Me.PrimarySSN.Value) _
Or IsNull(Me.PropAddress.Value) _
' additional Or conditions for each of next 5 fields
Or IsNull(Me.EstHomeValue.Value) Then
Me.AppStatus.Value = "RE PRE-QUAL"
Else
Me.AppStatus.Value = Null
End If
However that would be a bit unwieldy when extended to all 8 fields.
As an alternative, you could start with a list of the control names, load them into an array, and use a For loop to walk the array checking whether each control value is Null. If any of them is Null, set AppStatus to "RE PRE-QUAL" and break out of the loop.
Dim astrFields() As String
Dim strFieldList As String
Dim varAppStatus As Variant
Dim varField As Variant
strFieldList = "PrimarySSN,PropAddress,PropCity,PropState,PropZipCode," & _
"RequestedLoanAmount,BorrowerIncome,EstHomeValue"
astrFields = Split(strFieldList, ",")
varAppStatus = Null
For Each varField In astrFields
'Debug.Print varField
If IsNull(Me.Controls(varField).Value) = True Then
varAppStatus = "RE PRE-QUAL"
Exit For
End If
Next
Me.AppStatus.Value = varAppStatus
Notice this approach could make maintenance easier. If you ever need to add or remove controls from the list of those which should be examined, or change any of their names, simply edit the strFieldList string.
I think that this is what you need:
If [PrimarySSN].Value = Null Or [PropAddress].Value = Null Or [PropCity].Value = Null Or [PropState].Value = Null Or [PropZipCode].Value = Null Or [RequestedLoanAmount].Value = Null Or [BorrowerIncome.Value] = Null Or [EstHomeValue].Value = Null Then
[AppStatus].Value = "RE PRE-QUAL"
Else
[AppStatus].Value = Null
End If
And also consider this:
I am running the below expression to compare the diffence of 2 dates. If both dates are in the cell I would like it to return a 0 but if Date2 is blank I would like the difference to show. Right now I just get #ERROR if there is no date in date2. Any ideas would be greatly appreciated.
expr2: NetWorkDays([Date1],[Date2])
Option Compare Database
Public Function NetWorkdays(dteStart As Date, dteEnd As Date) As Integer
Dim intGrossDays As Integer
Dim dteCurrDate As Date
Dim i As Integer
intGrossDays = DateDiff("d", dteStart, dteEnd)
NetWorkdays = 0
For i = 0 To intGrossDays
dteCurrDate = dteStart + i
If Weekday(dteCurrDate, vbMonday) < 6 Then
End If
Next i
End Function
The function will always return 0 if you don't remove the NetWorkdays = 0 from the function, and you will continue you to get an error if you don't wrap where you call the function in an if statement like this:
If Not IsNull(Date2) Then
txtResult = NetWorkdays(Date1, Date2)
Else
txtResult = 0
End If
txtResult being the textbox you want to display your results in.
You may want to look at this link http://msdn.microsoft.com/en-us/library/bb258196(v=office.12).aspx. It is a function designed for access to calculate the number of workdays between two dates.
Using this function you should get the results you want doing something like this:
If Not IsNull(Date2) Then
txtResult = 0
Else
txtResult = Work_Days(Date1, Now())
End If