Help with datacontrol validation!!!

Help with datacontrol validation!!!

Post by Dave LaPor » Fri, 16 Jul 1999 04:00:00



        Could someone help me with the following problem.  I am
working on a project that entails adding new records,saving,finding
records to a access database ( with multiple relationships, and table
with composite key's ).
        The form that I am working on it a maintenance screen, to
populate base tables with data.  It is composed of 8 tabs, and each
tab contains text box's,combo box's, a data control for navigation of
the recordset, and command buttons to perform the following

cmdNew  == .Addnew records
cmdSave == .Update the recordset
cmdDelete == Delete a record
cmdFind == Find a record.

        I have written sub procedures to perform adding new
rec's,saving etc.  in the sub procedure for the update I have a simple
error trap that catch's any error thrown, cancels the update, and
informs the user that some data error occurred.  This seems to work.
But my problem is this.

Since I am dealing with a composite key situation, where I have 'for
example' 2 fields required,  I cannot allow the user to click the
cmdNew command button ( which calls .addnew ) and then move the
recordset pointer without filling out all the textbox's.  moving that
data control by accidently/or voluntary clicking on one of the error
buttons would start the save process, which causes an error and screws
up my app.  

How can I validate the form so that if the user ( after clicking
cmdNew ) clicks the data control, and try's to move the record pointer
without first filling in all the required fields...?

Here's my code for the .addnew,save,delete, and find.


Thanks.

*******************************************
Addnew procedure
*******************************************

Public Sub AddRec(DataCtl As Control, Focus As Control, Savebtn As
Control)

    gBookMark = DataCtl.Recordset.Bookmark

     ' Add a new Rec
    DataCtl.Recordset.MoveLast
    DataCtl.Recordset.AddNew

    ' Wipe out all text on the controls.
    DataCtl.Caption = "  Record: " &
(DataCtl.Recordset.AbsolutePosition + 2)
    ClearCtls
    Savebtn.Enabled = True

    Focus.Enabled = True

End Sub

*******************************************
Save procedure.
*******************************************

Public Sub SaveRec(DataCtl As Control, Focus As Control, Savebtn As
Control)

On Error GoTo ErrorHandler

    DataCtl.Recordset.Update

    ' Keep user from calling this procedure with out first
    ' hitting cmdNew, which calls the .Addnew procedure.
    Savebtn.Enabled = False

    'Set first control on form to current focus.
    Focus.SetFocus

ErrorHandler:
        MsgBox "Save Error - Check data entered", vbCritical, "ERROR"

        Focus.SetFocus
End Sub

***************************************
Delete procedure
***************************************

Public Sub Delete(DataCtl As Control, Focus As Control)

    Dim result As Long

    'Delete a record from table 'Base'
     If DataCtl.Recordset.RecordCount > 0 Then
        result = MsgBox("Delete Current Record?", vbYesNo +
vbQuestion, "Delet Record")
        If result = vbYes Then
            DataCtl.Recordset.Delete
            DataCtl.Recordset.MovePrevious
            DataCtl.Caption = "Record: " &
(DataCtl.Recordset.AbsolutePosition + 1)
        End If
     Else
        MsgBox "No Record to Delete!", vbInformation, "Delete"
     End If

     If DataCtl.Recordset.RecordCount > 0 Then
        DataCtl.Recordset.MoveFirst
        DataCtl.Caption = "Record: " &
(DataCtl.Recordset.AbsolutePosition + 1)
    End If

    Focus.SetFocus

End Sub

*******************************************
Find Procedure
******************************************

Public Sub Find(DataCtl As Control, Focus As Control)
     '
    Dim nResult As Integer
    Dim cFind As String
    Dim cBookMark As String
    '
    If DataCtl.Recordset.RecordCount > 0 Then
        cFind = InputBox("Enter Course Number:", "Search for a
Course")
        cFind = Trim(cFind)
        If cFind <> "" Then
            cBookMark = DataCtl.Recordset.Bookmark
            DataCtl.Recordset.FindFirst "cCourse_Num=" & cFind
            If DataCtl.Recordset.NoMatch Then
                MsgBox "Can't Find [" & cFind & "]", vbExclamation,
"Aborting Search"
                DataCtl.Recordset.Bookmark = cBookMark
            End If
        End If
    Else
        MsgBox "No Records to Search!", vbExclamation, "Search"
    End If
    Focus.SetFocus
    '
End Sub


 
 
 

Help with datacontrol validation!!!

Post by Jack » Fri, 16 Jul 1999 04:00:00


Here is some sample code, although it is for the ADO data control so I don't
know how much good it will do you:

Private Sub adoMaster_MoveComplete(ByVal adReason As ADODB.EventReasonEnum,
ByVal pError As ADODB.Error, adStatus As ADODB.EventStatusEnum, ByVal
pRecordset As ADODB.Recordset20)
    If Not adoMaster.Recordset.BOF And Not adoMaster.Recordset.EOF Then
        'Disables command buttons when adding new record
        If adoMaster.RecordSet.EditMode = adEditAdd
            cmdNew.Enabled = False
            cmdDelete.Enabled = False
            cmdFind.Enabled = False
        Else
            cmdNew.Enabled = True
            cmdDelete.Enabled = True
            cmdFind.Enabled = True
        End if
    End If
End Sub

Private Sub adoMaster_WillChangeRecord(ByVal adReason As
ADODB.EventReasonEnum, ByVal cRecords As Long, adStatus As
ADODB.EventStatusEnum, ByVal pRecordset As ADODB.Recordset20)
    'Cancels update if data not validated
    If adReason = adRsnUpdate Then
        If Not Data_Validate Then adStatus = adStatusCancel
    End If
End Sub

Private Sub adoMaster_WillMove(ByVal adReason As ADODB.EventReasonEnum,
adStatus As ADODB.EventStatusEnum, ByVal pRecordset As ADODB.Recordset20)
    'Cancels reposition if data not validated
    If Not adoMaster.Recordset.BOF And Not adoMaster.Recordset.EOF Then
        If Not Data_Validate Then adStatus = adStatusCancel
    End If
End Sub

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    'Cancels form unload if data not validated
    If Not Data_Validate Then Cancel = True
End Sub

Private Function Data_Validate() As Boolean
    'Validates data
    Data_Validate = True 'Sets default to true
    If Len(txtRequiredField1) = 0 Then
        Data_Validate = False
        MsgBox "Field1 required.", vbExclamation, Me.Caption
        txtRequiredField1.SetFocus
    End If
    If Len(txtRequiredField2) = 0 Then
        Data_Validate = False
        MsgBox "Field2 required.", vbExclamation, Me.Caption
        txtRequiredField2.SetFocus
    End If
End Function


> Could someone help me with the following problem.  I am
> working on a project that entails adding new records,saving,finding
> records to a access database ( with multiple relationships, and table
> with composite key's ).
> The form that I am working on it a maintenance screen, to
> populate base tables with data.  It is composed of 8 tabs, and each
> tab contains text box's,combo box's, a data control for navigation of
> the recordset, and command buttons to perform the following

> cmdNew  == .Addnew records
> cmdSave == .Update the recordset
> cmdDelete == Delete a record
> cmdFind == Find a record.

> I have written sub procedures to perform adding new
> rec's,saving etc.  in the sub procedure for the update I have a simple
> error trap that catch's any error thrown, cancels the update, and
> informs the user that some data error occurred.  This seems to work.
> But my problem is this.

> Since I am dealing with a composite key situation, where I have 'for
> example' 2 fields required,  I cannot allow the user to click the
> cmdNew command button ( which calls .addnew ) and then move the
> recordset pointer without filling out all the textbox's.  moving that
> data control by accidently/or voluntary clicking on one of the error
> buttons would start the save process, which causes an error and screws
> up my app.

> How can I validate the form so that if the user ( after clicking
> cmdNew ) clicks the data control, and try's to move the record pointer
> without first filling in all the required fields...?

> Here's my code for the .addnew,save,delete, and find.


> Thanks.

> *******************************************
> Addnew procedure
> *******************************************

> Public Sub AddRec(DataCtl As Control, Focus As Control, Savebtn As
> Control)

>     gBookMark = DataCtl.Recordset.Bookmark

>      ' Add a new Rec
>     DataCtl.Recordset.MoveLast
>     DataCtl.Recordset.AddNew

>     ' Wipe out all text on the controls.
>     DataCtl.Caption = "  Record: " &
> (DataCtl.Recordset.AbsolutePosition + 2)
>     ClearCtls
>     Savebtn.Enabled = True

>     Focus.Enabled = True

> End Sub

> *******************************************
> Save procedure.
> *******************************************

> Public Sub SaveRec(DataCtl As Control, Focus As Control, Savebtn As
> Control)

> On Error GoTo ErrorHandler

>     DataCtl.Recordset.Update

>     ' Keep user from calling this procedure with out first
>     ' hitting cmdNew, which calls the .Addnew procedure.
>     Savebtn.Enabled = False

>     'Set first control on form to current focus.
>     Focus.SetFocus

> ErrorHandler:
>         MsgBox "Save Error - Check data entered", vbCritical, "ERROR"

>         Focus.SetFocus
> End Sub

> ***************************************
> Delete procedure
> ***************************************

> Public Sub Delete(DataCtl As Control, Focus As Control)

>     Dim result As Long

>     'Delete a record from table 'Base'
>      If DataCtl.Recordset.RecordCount > 0 Then
>         result = MsgBox("Delete Current Record?", vbYesNo +
> vbQuestion, "Delet Record")
>         If result = vbYes Then
>             DataCtl.Recordset.Delete
>             DataCtl.Recordset.MovePrevious
>             DataCtl.Caption = "Record: " &
> (DataCtl.Recordset.AbsolutePosition + 1)
>         End If
>      Else
>         MsgBox "No Record to Delete!", vbInformation, "Delete"
>      End If

>      If DataCtl.Recordset.RecordCount > 0 Then
>         DataCtl.Recordset.MoveFirst
>         DataCtl.Caption = "Record: " &
> (DataCtl.Recordset.AbsolutePosition + 1)
>     End If

>     Focus.SetFocus

> End Sub

> *******************************************
> Find Procedure
> ******************************************

> Public Sub Find(DataCtl As Control, Focus As Control)
>      '
>     Dim nResult As Integer
>     Dim cFind As String
>     Dim cBookMark As String
>     '
>     If DataCtl.Recordset.RecordCount > 0 Then
>         cFind = InputBox("Enter Course Number:", "Search for a
> Course")
>         cFind = Trim(cFind)
>         If cFind <> "" Then
>             cBookMark = DataCtl.Recordset.Bookmark
>             DataCtl.Recordset.FindFirst "cCourse_Num=" & cFind
>             If DataCtl.Recordset.NoMatch Then
>                 MsgBox "Can't Find [" & cFind & "]", vbExclamation,
> "Aborting Search"
>                 DataCtl.Recordset.Bookmark = cBookMark
>             End If
>         End If
>     Else
>         MsgBox "No Records to Search!", vbExclamation, "Search"
>     End If
>     Focus.SetFocus
>     '
> End Sub




 
 
 

1. Help, js form validation vs sql validation?

I've recently been saddled with the task of learning SQL and modifying
existing code.  This task in particular.  From what I can see of the power
of SQL my problem should be easy to solve.

It's an 'AddNewUser.asp' type of thing.
Currently, on loading the page, it queries the database and populates a
JavaScript form validation with all the existing usernames in the DB.  Then,
if you attempt to add a name that already exists, it prompts you to choose
another name. (fine if you've got 20 names, but we're approaching 2000! I've
attached the ASP/JS code below)  This is causing the page to take a long
time to load initially, then an equally long time to load once you've
clicked 'Add User.'  Again, not a real big deal... unless you need to add
more than one user a day.

I believe there MUST be a better, faster, stronger way to do this.  But,
being the novice, don't know exactly how to begin this task.

Could someone please point me in the proper direction?  (aside from hiring
an expert on this.  I'd like to try to use it to learn more about how this
stuff works ;-)

/* code below */
/* other form validation stuff */
  :
/* loop through all recordsets & populate validation fields */
 <%for each counter in userArr%>
  if (document.frmed.UserName.value == "<%=counter%>" && "<%=counter%>" !=
"<%=rs.Fields("username")%>")
  {
   alert("Username you selected already exists in the database!");
   document.frmed.UserName.focus();
   document.frmed.UserName.select();
   return false;
  }
 <%next%>

/* continue with other element validations */
 :
/* other form validation stuf */
/* end code snippet */

2. WA-SEATTLE-87856--Database Design-C++-MS SQL Server-ORACLE-Information Systems E

3. Global validation table vs. separate validation tables

4. Best database for WEB site - Help !

5. Database, datacontrol -Need your help!

6. Database Gateways

7. Help with the Datacontrol....

8. Passing values to the IN (T-SQL) Clause

9. HELP with datacontrol

10. Please Help ( SQL /DataControl )

11. Plea for help ref datacontrol - thanks

12. TrueGrid recordsource via DataControl -HELP

13. Help --- Parameter Query Using DataControl