Full Version: validating form using sql
UtterAccess Discussion Forums > Microsoft® Access > Access Forms
inventingdreams
I would like to write some code and I can't seem to come up with a good structure.
Here's what I want to happen. When the submit button is pressed I would like
If (There are any entries with Me.frmDate and Me.Employee in the TimeCard table)
Prompt (Do you want to override current data?)
if yes (delete all entries (2 entries) with specified data and replace with new entries)
if no (do not update fields, but leave forms current position)
Else (use INSERT INTO sql to enter Me.alltheentries into the table)<-This part is already programmed

I know pretty well how to write the code incorporating sql into vba but I just am having a hard time wrapping my head around how to structure this.
dashiellx2000
Here some code that I use for one of my dbs to do what you are asking.
CODE
Dim TechID As String
Dim TotalHours As String
Dim ProjectHours As String
Dim HistoryMonth As String
Dim Dup As String
Dim ViewID As String
Dim varWhere As Variant
Dim QD As QueryDef
Dim db As Database
Dim rst As Recordset

TechID = Me.TechID
TotalHours = Me.HoursWorked
ProjectHours = Me.ProjectHours
HistoryMonth = Me.StartDate

Me.PreviouslyEntered = Nz(DLookup("HistoryID", "tblHistory", "TechID =" & TechID & "AND HistoryMonth =#" & HistoryMonth & "#"))
If IsNull(Me.PreviouslyEntered) Or Me.PreviouslyEntered = "" Then
    Dup = 0
    Else
    Dup = Me.PreviouslyEntered
End If

If Me.PreviouslyEntered <> 0 Then
    Select Case MsgBox("Productivity Data has already been entered for this Tech for the month indicated!" & vbCrLf & _
    "Do you want to REPLACE that Data with date currently entered?", vbYesNo + vbQuestion, "Already Entered")
    Case vbYes
    Set db = CurrentDb()
    db.Execute ("UPDATE tblHistory SET tblHistory.TotalHours = " & TotalHours & ", tblHistory.ProjectHours = " & ProjectHours & "," & _
    " tblHistory.HistoryMonth = #" & HistoryMonth & "#" & _
    " WHERE (((tblHistory.HistoryID)=" & Dup & "));")
    Case vbNo
        MsgBox "Please pull reports for unchanged data from the Tech Report Menu", vbInformation, "Incorrect Menu"
        DoCmd.OpenForm "frmTechReports", , , , , , Me.Name
        Me.Visible = False
        Exit Sub
    End Select
Else

Set db = CurrentDb()

Set rst = db.OpenRecordset("tblHistory")

rst.AddNew
rst!TechID = TechID
rst!TotalHours = TotalHours
rst!ProjectHours = ProjectHours
rst!HistoryMonth = HistoryMonth

rst.Update
rst.close

Set rst = Nothing

End If


HTH.
inventingdreams
cool
thats about right
thanks
dashiellx2000
Glad I could help.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.