Hello, geniuses of Access.
I am trying to teach myself Access VBA coding to rework a tracker database that has been destroyed over the past year. The database consists of a single table containing some 2200 records with the following columns: LastName, FirstName, MI, Ssno, MOB, Med Status, Med Date Modified, Med Date Due, Dent Status, Dent Date Modified, Dent Date Due, Memo, Updated By, Last4SSno, IDprimary key.
The code I'm trying to run is running on a form that has the fields: txtSSno, txtRecordType, txtLocation, txtName, txtMemo.
The objective of the form is to set the location and record type fields to where ever the record is located and which record type(M for medical, D for dental) is being scanned, then lookup the social in the table, match it to the social being scanned, and then update the record in the table with the proper information. I have spent almost a week solid now google searching and reading everything I could get my hands on to find the answers, but I'm stumped. Here is the code:
I also want to remove the ID field from the table and make the Ssno the primary key, since no two people should have the same social security number.
Take it with a grain of salt that the code is not going to be elegant, I'm learning and any explanation of WHY you chose to present a solution will be very much appreciated.
Thanks in advance,
Shen
I am trying to teach myself Access VBA coding to rework a tracker database that has been destroyed over the past year. The database consists of a single table containing some 2200 records with the following columns: LastName, FirstName, MI, Ssno, MOB, Med Status, Med Date Modified, Med Date Due, Dent Status, Dent Date Modified, Dent Date Due, Memo, Updated By, Last4SSno, IDprimary key.
The code I'm trying to run is running on a form that has the fields: txtSSno, txtRecordType, txtLocation, txtName, txtMemo.
The objective of the form is to set the location and record type fields to where ever the record is located and which record type(M for medical, D for dental) is being scanned, then lookup the social in the table, match it to the social being scanned, and then update the record in the table with the proper information. I have spent almost a week solid now google searching and reading everything I could get my hands on to find the answers, but I'm stumped. Here is the code:
Code:
Option Compare Database
Private Sub Form_Activate()
Me.txtSSno = Nothing
Me.txtName = Nothing
Me.TxtMemo = Nothing
Me.txtSSno.SetFocus
End Sub
Private Sub txtSSno_AfterUpdate()
Dim rsTemp As ADODB.Recordset
On Error Resume Next
Err.Clear
Set rsTemp = New ADODB.Recordset
rsTemp.Open "SELECT * from MedicalRecords.table WHERE Ssno = " & Me.txtSSno & "", CurrentProject.Connection, adOpenDynamic, adLockOptimistic, adCmdText
If Err.Number <> 0 Then
' MsgBox "Social not on file. Click on Menu, then Add a new record, and return here.", vbInformation, "Missing Number!"
MsgBox "Error" & Err
Exit Sub
End If
rsTemp.MoveFirst
Me.txtName = rsTemp.Fields("lastname") & ", " & rsTemp.Fields("firstname") & "; [" & rsTemp.Fields("Ssno") & "] "
rsTemp.Close
Set rsTemp = Nothing
DoCmd.SetWarnings False
If Me.txtLocation = "IN" And Me.txtRecordType = "M" Then
DoCmd.RunSQL "UPDATE MedicalRecords SET MedStatus='IN', MedOut='', MedDue='', Memo='" & Me.TxtMemo & "', WHERE MedicalRecords.Ssno='" & Me.txtSSno & "'"
ElseIf Me.txtLocation = "IN" And Me.txtRecordType = "D" Then
DoCmd.RunSQL "UPDATE MedicalRecords SET DenStatus='IN',DenOut='',DenDue='',Memo='" & Me.TxtMemo & "', WHERE MedicalRecords.Ssno='" & Me.txtSSno & "'"
ElseIf Me.txtLocation = "OUT" And Me.txtRecordType = "M" Then
DoCmd.RunSQL "UPDATE MedicalRecords SET MedStatus='OUT',MedOut= #" & Now() & "#,MedDue=#" & DateAdd("d", 14, Now()) & "#,Memo='" & Me.TxtMemo & "', WHERE MedicalRecords.Ssno='" & Me.txtSSno & "'"
ElseIf Me.txtLocation = "OUT" And Me.txtRecordType = "D" Then
DoCmd.RunSQL "UPDATE MedicalRecords SET DenStatus='OUT',DenOut= #" & Now() & "#,DenDue=#" & DateAdd("d", 14, Now()) & "#,Memo='" & Me.TxtMemo & "', WHERE MedicalRecords.Ssno='" & Me.txtSSno & "'"
ElseIf Me.txtLocation <> "IN" And Me.txtLocation <> "OUT" And Me.txtRecordType = "M" Then
DoCmd.RunSQL "UPDATE MedicalRecords SET MedStatus='" & Me.txtLocation & "', MedOut= '" & Now() & "', MedDue='" & DateAdd("d", 14, Now()) & "', Memo='" & Me.TxtMemo & "', WHERE Ssno='" & Me.txtSSno & "'"
MsgBox ("Error" & Err)
ElseIf Me.txtLocation <> "IN" And Me.txtLocation <> "OUT" And Me.txtRecordType = "D" Then
DoCmd.RunSQL "UPDATE MedicalRecords SET DenStatus='" & Me.txtLocation & "',DenOut= #" & Now() & "#,DenDue=#" & DateAdd("d", 14, Now()) & "#,Memo='" & Me.TxtMemo & "', WHERE MedicalRecords.Ssno='" & Me.txtSSno & "'"
End If
DoCmd.SetWarnings True
Me.txtSSno = ""
Me.txtName = ""
Me.TxtMemo = ""
Me.txtSSno.SetFocus
Err_txtConfirm_Click:
MsgBox Err.Description
End Sub
I also want to remove the ID field from the table and make the Ssno the primary key, since no two people should have the same social security number.
Take it with a grain of salt that the code is not going to be elegant, I'm learning and any explanation of WHY you chose to present a solution will be very much appreciated.
Thanks in advance,
Shen