coding compare data from txtbox with info in table

  • Hi

    I'm tring to get the user to enter the correct information once they click on the cmdSave button.

    The data in the field can be changed but must link to the table or query. I keep getting the msgbox even if the data exists.

    Please HELP

    Private Sub cmdSave_Click()

        If Me.ClinicianID.Value <> "qryPatientCheck.ClinicianID" Then

        MsgBox "Please Type in a valid ID as data will not be saved"

        End If

       

    End If

    If Me.Dirty = True Then

        If MsgBox("Do you want to save the changes made?", vbYesNo, "Save") = vbYes Then

                    DoCmd.RunCommand acCmdSaveRecord 'save option - if yes save else do nothing

               

        End If

     End If

    end sub

  • This was removed by the editor as SPAM

  • What version of Access are you using?

    Are you using ado, dao,...?

  • access 2000.

    I'm trying to use d lookup. But Its not hitting the first if condition even if its true. HELP

    If Me.Dirty = True Then

        If Forms![frmPatientCheck]![ClinicianID].Value <> DLookup("ClinicianID", "tblClinician", "ClinicianID = " & Me!ClinicianID) Then

            MsgBox "Please enter the correct Clinician ID as data will only then be saved "

     

        End If

        If Forms![frmPatientCheck]![ClinicianID].Value = DLookup("ClinicianID", "tblClinician", "ClinicianID = " & Me!ClinicianID) Then

           MsgBox "ok"

      '  DoCmd.RunCommand acCmdSaveRecord 'save option - if yes save else do nothing

      End If

    End If

     

  • It looks like you should try using a combobox that displays a list of acceptable "ClinicianID" values, and set the combo "LimitToList" property to true, so no other data can be added. Then all you have to do is check that the combobox is not null e.g:

    If isnull(me.cboPatientCheckID) then

    me.cboPatientCheckID.setfocus

    msgbox "please enter a valid ID blah, blah"

    End If

    The rowsouce for the combobox would be something like "SELECT ClinicanID FROM tblClinican" . You could add other columns in here to make the selection easier.

    Hope this helps.

  • thanks but i need to have it as a txtbox

  • Ok, how about opening the table and check that the ID exists using ADO. e.g.

    DIm rst as ADODB.recordset

    dim varVal as variant

    Dim strmsg as string

    dim strSQL as string

    varVal = me.txtMyID

    If isnull(me.txtMyID) then

    strmsg = "You must enter a valid ID etc"

    else

    strSQL = "SELECT MyID FROM tblMyTable WHERE MyID = " & varVal

    'you will need to add some more quotes if the ID is text based

    set rst = new adodb.recordset

    rst.open strSQL, currentproject.connection, adopenforwardonly, adlockreadonly, adcmdtext

    if rst.bof and rst.eof then

    strmsg = "ID not found, you must enter a valid ID"

    end if

    rst.close

    set rst = nothing

    If len(strMsg) > 0 then

    me.txtMyID.setfocus

    msgbox strMsg

    End If

    I have assumed that the connection is the current project but that could be changed to any data connection and I have used some dynamic SQL, which may or may not be acceptable depending on your circumstance. This could be modified to be safer if the user input is untrusted.

  • Sorry, i think i left out an End IF at the end of my eg.

Viewing 8 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic. Login to reply