Solved

MS Access VBA Questions

Posted on 2013-01-08
8
236 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 120

Accepted Solution

by:
Rey Obrero (Capricorn1) 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
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 
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
 

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 120

Assisted Solution

by:Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1) 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 120

Assisted Solution

by:Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1) 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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Office 365 home questions 7 65
type of query 11 42
Access on Mouse move 5 35
combo box query 6 8
I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…

803 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