Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Duplicate Data

Posted on 2005-04-26
5
Medium Priority
?
157 Views
Last Modified: 2011-09-20
Please Note: I am trying to add Dept Num to a table. If the Dept Num exists in the table, I want a message to be displayed on the form that the Dept Num number exists.

 There are only two fields on the form DeptNum.text and DeptDesc.text.  I am using Data1 as Recordset.

 The code some what works, only when it gets to message 'Department Already Exists' , if the Dept      Num exists in the table, instead of the message popping up, the program crasches displaying the default run-time error '3022' instead.  What am I doing wrong? Please help


Private Sub cmdSaveForm_Click()

If frmAddDept.txtDeptNum = "" Then
        Msg = "The Department Number Must Be Entered! "
        MsgResult = DoMsgBox(cur_frm_obj, Msg, vbOKOnly + vbSystemModal, "Required Data")
        txtDeptNum.SetFocus
        Exit Sub
End If

If frmAddDept.txtDeptDesc = "" Then
        Msg = "The Department Description Must Be Entered! "
        MsgResult = DoMsgBox(cur_frm_obj, Msg, vbOKOnly + vbSystemModal, "Required Data")
        txtDeptDesc.SetFocus
        Exit Sub
End If

SQLString = "'" & txtDeptNum.Text & "'"
SQLQuery = "SELECT deptnum FROM tblDepartments WHERE (((tblDepartments.deptnum) LIKE '" _
      & txtDeptNum.Text & "' ORDER BY deptnum"

        Data1.Recordset.AddNew
        Data1.Recordset.Fields("DeptNum") = frmAddDept.txtDeptNum.Text
        Data1.Recordset.Fields("DeptDesc") = frmAddDept.txtDeptDesc.Text

If Data1.Recordset.Fields("DeptNum") = SQLQuery Then 'txtDeptNum.Text Then
        Msg = "The Department Number Already Exists! "
       MsgResult = DoMsgBox(cur_frm_obj, Msg, vbOKOnly + vbSystemModal, "Duplicate Data")
       txtDeptNum.SetFocus
      Exit Sub
Else
        Data1.Recordset.Update
   
        txtDeptNum.Text = ""
        txtDeptDesc.Text = ""
        txtDeptNum.SetFocus
        Exit Sub
End If
 
End Sub
0
Comment
Question by:Fadennis
2 Comments
 
LVL 8

Accepted Solution

by:
wraith821 earned 2000 total points
ID: 13871058
Go about it a different way,

1. get the DeptNum from the form.
2. Query the tblDepartments for the Number first -  "SELECT deptnum FROM tblDepartments WHERE (((tblDepartments.deptnum) LIKE '" & txtDeptNum.Text & "' ORDER BY deptnum"
3. if you pull back a record, just display "The Department Number Already Exists!" msgbox.
4. if nothing comes back on that number then do the Addnew, Field='', update

you'll have more control that way.


0
 
LVL 12

Expert Comment

by:Preece
ID: 13881256
Perhaps you need to do the update before evaluating?

        Data1.Recordset.AddNew
        Data1.Recordset.Fields("DeptNum") = frmAddDept.txtDeptNum.Text
        Data1.Recordset.Fields("DeptDesc") = frmAddDept.txtDeptDesc.Text

        Data1.Recordset.Update

If Data1.Recordset.Fields("DeptNum") = SQLQuery Then 'txtDeptNum.Text Then
...


0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

564 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