Solved

MS Access VBA Questions

Posted on 2013-01-08
8
211 Views
Last Modified: 2013-01-08
The code below inserts start and end dates for a employee with a reason to produce a report that lists events.

How do I add the following features to the code below?

If cboemp (employee is not selected) display message " Please select employee"
If cboreason (reason is not selected) display message " Please select reason"
If txtEnd (end date is not selected) display message " Please select end date"
If txtStart (start date is not selected) display message " Please select start date"

I.E. Don't perform any action until all fields are filled in.

I would also like to add a delete button to delete records according to start and end date.


Private Sub btnUpdate_Click()
Dim strSQL As String
Dim dtmNextDate As Date
dtmNextDate = Me.txtStart
    Do Until dtmNextDate = Me.txtEnd + 1
        strSQL = "INSERT INTO tblEmpTimeOff (etoDate,eto_EmpID,etoReasonCode) VALUES(" & "#" & dtmNextDate & "#" & ", " & Me.cboEmp & ", " & Me.cboReason & ")"
        CurrentDb.Execute strSQL, dbFailOnError
        dtmNextDate = dtmNextDate + 1
    Loop
    Me.cboEmp = ""
    Me.cboReason = ""
    Me.txtEnd = ""
    Me.txtStart = ""
    MsgBox "Input Recieved, Thank You"
End Sub

Open in new window

0
Comment
Question by:DJPr0
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 119

Accepted Solution

by:
Rey Obrero earned 400 total points
ID: 38754745
try this

Private Sub btnUpdate_Click()
Dim strSQL As String
Dim dtmNextDate As Date

if Me.cboEmp.listindex=-1 then
	msgbox " Please select employee"
	me.cboEmp.setfocus
	exit sub
end if

if Me.cboReason.listindex=-1 then
	msgbox " Please select reason"
	me.cboReason.setfocus
	exit sub
end if

if me.txtEnd & ""="" then
   msgbox " Please select end date"
   me.txtend.setfocus
   exit sub

end if

if me.txtstart & ""="" then
   msgbox " Please select start date"
   me.txtstart.setfocus
   exit sub

end if

dtmNextDate = Me.txtStart
    Do Until dtmNextDate = Me.txtEnd + 1
        strSQL = "INSERT INTO tblEmpTimeOff (etoDate,eto_EmpID,etoReasonCode) VALUES(" & "#" & dtmNextDate & "#" & ", " & Me.cboEmp & ", " & Me.cboReason & ")"
        CurrentDb.Execute strSQL, dbFailOnError
        dtmNextDate = dtmNextDate + 1
    Loop
    Me.cboEmp = ""
    Me.cboReason = ""
    Me.txtEnd = ""
    Me.txtStart = ""
    MsgBox "Input Recieved, Thank You"
End Sub

Open in new window

0
 
LVL 61

Expert Comment

by:mbizup
ID: 38754756
Try this...

Private Sub btnUpdate_Click()
Dim strSQL As String
Dim dtmNextDate As Date
dtmNextDate = Me.txtStart

If cboemp & "" = "" then

     msgbox  " Please select employee"
     me.cboemployee.setfocus
      exit sub
end if

If cboreason & "" = "" then

     msgbox  " Please select reason"
     me.cboreason.setfocus
     exit sub

end if

If txtEnd & "" = "" then

     msgbox  " Please select end date"
     me.txtEnd.setfocus
      exit sub

end if

If txtStart & "" = "" then

     msgbox  " Please select start date"
     me.txtStart.setfocus
      exit sub

end if



    Do Until dtmNextDate = Me.txtEnd + 1
        strSQL = "INSERT INTO tblEmpTimeOff (etoDate,eto_EmpID,etoReasonCode) VALUES(" & "#" & dtmNextDate & "#" & ", " & Me.cboEmp & ", " & Me.cboReason & ")"
        CurrentDb.Execute strSQL, dbFailOnError
        dtmNextDate = dtmNextDate + 1
    Loop
    Me.cboEmp = ""
    Me.cboReason = ""
    Me.txtEnd = ""
    Me.txtStart = ""
    MsgBox "Input Recieved, Thank You"
End Sub 

Open in new window

0
 
LVL 61

Assisted Solution

by:mbizup
mbizup earned 50 total points
ID: 38754794
<<I would also like to add a delete button to delete records according to start and end date.>>

The Delete button code would be the same overall.  You'd just need to use a different SQL String:


       
strSQL = "DELETE * FROM  tblEmpTimeOff WHERE etoDate BETWEEN #"  & me.txtStart & "# AND #" & me.txtEnd & "#"  

Open in new window

0
 
LVL 77

Assisted Solution

by:peter57r
peter57r earned 50 total points
ID: 38754815
Giving users such a powerful delete button is asking for trouble.  I'd think twice about about how you deal with that situation.  

In general, I don't allow users to delete anything and simply mark records as 'deleted' and exclude them from all data sources.
0
Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

 

Author Comment

by:DJPr0
ID: 38754898
Is mark records as deleted different from this statement:
strSQL = "DELETE * FROM  tblEmpTimeOff WHERE etoDate BETWEEN #"  & me.txtStart & "# AND #" & me.txtEnd & "#"  

Open in new window


Does mark records as deleted - have a restoring option?
0
 
LVL 119

Assisted Solution

by:Rey Obrero
Rey Obrero earned 400 total points
ID: 38754922
<Does mark records as deleted - have a restoring option? >

Yes,

But to mark records, you will use an update query, something like this

strSql="Update tblEmpTimeOff set [nameoffield]='InActive' WHERE etoDate BETWEEN #"  & me.txtStart & "# AND #" & me.txtEnd & "#"
0
 
LVL 119

Assisted Solution

by:Rey Obrero
Rey Obrero earned 400 total points
ID: 38754933
or use a Yes/No field named [InActive]

strSql="Update tblEmpTimeOff set [InActive]= -1 WHERE etoDate BETWEEN #"  & me.txtStart & "# AND #" & me.txtEnd & "#"

to restore the records, just reverse the process

strSql="Update tblEmpTimeOff set [InActive]= 0 WHERE etoDate BETWEEN #"  & me.txtStart & "# AND #" & me.txtEnd & "#"
0
 

Author Closing Comment

by:DJPr0
ID: 38754995
Thanks capricorn1!
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now