Validation Help, (how to detect if already have same data)

Thread Solved

Join Date: Jul 2007
Posts: 102
Reputation: hawisme000 is an unknown quantity at this point 
Solved Threads: 2
hawisme000's Avatar
hawisme000 hawisme000 is offline Offline
Junior Poster

Validation Help, (how to detect if already have same data)

 
0
  #1
Dec 28th, 2007
ummm, i got a for that is connected to a database, and have an

add, edit, delete cmd buttons.

if example i have "MyName" already on a certain field of a table on a database,

then on my form i wanna add "MyName" also,

what i want is there will be a msgbox that will tell theres already "MyName" on my table,

-------
also if i add into the table

"MyNaMe" or "MYNAME" (have diff Uppercase and Lowercase charater)
is will still recognize it as a simple "MyName"


pls help me on this one.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 537
Reputation: choudhuryshouvi is an unknown quantity at this point 
Solved Threads: 49
choudhuryshouvi's Avatar
choudhuryshouvi choudhuryshouvi is offline Offline
Posting Pro

Re: Validation Help, (how to detect if already have same data)

 
0
  #2
Dec 28th, 2007
check out this application logic. assuming that the database used by you is an access database. the db here used is "employee", table "details" with one field "empname". (the Green Lines are comments)

Option Explicit

Dim db As Database
Dim rs As Recordset

''performing the addition operation with the validation
Private Sub Command1_Click()
If Trim(txtempname.Text) <> "" Then
    If IsEmpExists(Trim(txtempname.Text)) Then
        MsgBox "The employee " & Chr(34) & Trim(txtempname.Text) & Chr(34) & " already exists in the database." & vbCrLf & _
            "Please use a different name rather than this one.", vbInformation, "Duplicate Employee"
        txtempname.SelStart = 0
        txtempname.SelLength = Len(Trim(txtempname.Text))
        txtempname.SetFocus
    Else
        rs.AddNew
        If rs.EditMode = dbEditAdd Then
            rs!empname = Trim(txtempname.Text)
            rs.Update
            Call LoadEmployees
            MsgBox "New employee created.", vbInformation, "Done"
            txtempname.Text = ""
            txtempname.SetFocus
        End If
    End If
Else
    MsgBox "The employee name should not be null.", vbCritical
    txtempname.SetFocus
End If
End Sub

Private Sub Form_Load()
''making connection to the database and opening the employee table to retrieve existing records
Set db = OpenDatabase(App.Path & "\employee.mdb")
Set rs = db.OpenRecordset("details", dbOpenTable)

If rs.RecordCount > 0 Then
    rs.MoveFirst
Else
    MsgBox "No employee details found." & vbCrLf & _
        "Please add some records.", vbInformation
End If
''creating the list of existing employees
Call LoadEmployees
End Sub

''this is the core function which is responsible for the entire validation process to be executed. this function takes
''the employee name as an arguement that you wish to check whether already exists in the database or not. the function
''checks for its first occurance. if found then it returns true otherwise returns false. "true" means the employee
''does already exist and "false" means not.
Public Function IsEmpExists(ByVal e_name As String) As Boolean
Dim rsd As Recordset    ''declarigng a recordset object specially for this operation
Dim cur_emp As String   ''this will hold current value returned by the recordset object while scanning through tha table

Set rsd = db.OpenRecordset("details", dbOpenTable)  'pointing the recordset to the table
If rsd.RecordCount > 0 Then     'checking if there is any record exists in the table. if its found any record then
                                'the recordcount should be greater than zero.
    rsd.MoveFirst   'pointing 'rsd' to the first record in the 'table. this actually notifies that a in-scanning process
                    'through the table is about to begin.
    While Not rsd.EOF()     'looping through the table until the end of file is reached
        cur_emp = rsd!empname   'parsing current cell value of 'empname' to the string var. 'cur_emp'
        If UCase(cur_emp) = UCase(e_name) Then      'now checking whether 'cur_emp' and the parameter passed does have
            IsEmpExists = True                      'same value or not. if yes then employee already exists. so returns
            Exit Function                           'true. once it becomes true there is no need of scanning again.so
        End If                                      'exists the function.
        rsd.MoveNext
    Wend
    IsEmpExists = False     'if no matching is found between 'cur_emp' and 'e_name' the function returns false,means there
                            'is no such employee exists with same name in the table.so you can proceed to add it into the
                            'database
Else    'there is no existing record found, so returns false
    IsEmpExists = False
End If
End Function

''this is just a sub-routine to call all existing employee records from the database and add them to the listbox. this
''becomes very useful in case of side-by-side viewing. you can easily see that the validation is going all ok.
Public Sub LoadEmployees()
Dim rsx As Recordset

Set rsx = db.OpenRecordset("details", dbOpenTable)
List1.Clear

If rsx.RecordCount > 0 Then
    rsx.MoveFirst
    While Not rsx.EOF()
        With List1
            .AddItem rsx!empname
            .ListIndex = .ListCount - 1
        End With
        rsx.MoveNext
    Wend
    Exit Sub
End If
List1.AddItem "-=No Existing Employees=-"
End Sub

know me if this helps you.

regards
Shouvik
Last edited by choudhuryshouvi; Dec 28th, 2007 at 1:12 pm.
Shouvik_The_Expert_Coder
Have a problem? Don't worry just give me a call and I'll fix it for you.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 218
Reputation: hkdani is an unknown quantity at this point 
Solved Threads: 24
hkdani's Avatar
hkdani hkdani is offline Offline
Posting Whiz in Training

Re: Validation Help, (how to detect if already have same data)

 
0
  #3
Dec 28th, 2007
what i want is there will be a msgbox that will tell theres already "MyName" on my table,
Don't really understand what you want?

If you already have accessed the data, you should already know that the MyName Field exists.

"MyNaMe" or "MYNAME" (have diff Uppercase and Lowercase charater)
is will still recognize it as a simple "MyName"
Microsoft doesn't care if you use Uppercase or Lower Case when referring to field names in a database. It just cares if the letters match.

Hank
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 102
Reputation: hawisme000 is an unknown quantity at this point 
Solved Threads: 2
hawisme000's Avatar
hawisme000 hawisme000 is offline Offline
Junior Poster

Re: Validation Help, (how to detect if already have same data)

 
0
  #4
Dec 29th, 2007
im now trying the logic of your example sir shouvi, wish me luck
thx for the help

@hkDani

OPsss sori, i got confused , what i wanna say is about the data inside a field

umm ok here goes.

if theres a "Haw" already inside the field "Username"

then i wanna add "HAW" (all CAPS)

a validation msgbox will say that theres already a data with the same name.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 218
Reputation: hkdani is an unknown quantity at this point 
Solved Threads: 24
hkdani's Avatar
hkdani hkdani is offline Offline
Posting Whiz in Training

Re: Validation Help, (how to detect if already have same data)

 
0
  #5
Dec 29th, 2007
if theres a "Haw" already inside the field "Username"

then i wanna add "HAW" (all CAPS)
Hah, hah. That's really funny

You have earned my first official Good Grief!

Hah, hah, hah.

I can't stop laughing!
Last edited by hkdani; Dec 29th, 2007 at 12:41 am.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 102
Reputation: hawisme000 is an unknown quantity at this point 
Solved Threads: 2
hawisme000's Avatar
hawisme000 hawisme000 is offline Offline
Junior Poster

Re: Validation Help, (how to detect if already have same data)

 
0
  #6
Dec 29th, 2007
sorry if u cnt get what iwanna tell u,
(cause im not that good at expressing in english )

currently converting what sir shouvi gave me into an ADO, (really wish me luch)
Last edited by hawisme000; Dec 29th, 2007 at 12:44 am.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 218
Reputation: hkdani is an unknown quantity at this point 
Solved Threads: 24
hkdani's Avatar
hkdani hkdani is offline Offline
Posting Whiz in Training

Re: Validation Help, (how to detect if already have same data)

 
0
  #7
Dec 29th, 2007
Save both values into 2 different variables.

Convert both to uppercase. Test to see if they're true. When in doubt. Use your immediate window: <Ctl +G>

This is false
----------------------------------------
Immediate
?"Hah"="HAH"
False


This is true
----------------------------------------
Immediate
?"HAH"="HAH"
True


--------------------
Immediate

?"GOOD GRIEF"="Good Grief"
False

?(ucase("GOOD GRIEF")=ucase("Good Grief"))
True
Last edited by hkdani; Dec 29th, 2007 at 12:50 am.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 102
Reputation: hawisme000 is an unknown quantity at this point 
Solved Threads: 2
hawisme000's Avatar
hawisme000 hawisme000 is offline Offline
Junior Poster

Re: Validation Help, (how to detect if already have same data)

 
0
  #8
Dec 29th, 2007
ic, why dnt' i think of that simple logic,

well now i know ,
im starting to laugh at myself to,

ok back to converting
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 102
Reputation: hawisme000 is an unknown quantity at this point 
Solved Threads: 2
hawisme000's Avatar
hawisme000 hawisme000 is offline Offline
Junior Poster

Re: Validation Help, (how to detect if already have same data)

 
0
  #9
Dec 29th, 2007
IVE SOLVED MY PROB THX to you to,

ive finished converting it to ADO

here what ive finished,

really thx esp to sir shouvi

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Option Explicit
  2.  
  3.  
  4. Dim rs As ADODB.Recordset
  5. Dim Conn As ADODB.Connection
  6. Dim esql As String
  7.  
  8. ''this is the core function which is responsible for the entire validation process to be executed. this function takes
  9. ''the employee name as an arguement that you wish to check whether already exists in the database or not. the function
  10. ''checks for its first occurance. if found then it returns true otherwise returns false. "true" means the employee
  11. ''does already exist and "false" means not.
  12.  
  13.  
  14. Public Function IsEmpExists(ByVal e_name As String) As Boolean
  15. Dim rsd As New ADODB.Recordset ''declarigng a recordset object specially for this operation
  16. Dim cur_emp As String ''this will hold current value returned by the recordset object while scanning through tha table
  17.  
  18. Set rsd = New ADODB.Recordset 'pointing the recordset to the table
  19. rsd.CursorType = adOpenStatic
  20. rsd.CursorLocation = adUseClient
  21. rsd.LockType = adLockPessimistic
  22. rsd.Source = ("Select * From tblStudacct")
  23. rsd.ActiveConnection = Conn
  24. rsd.Open
  25.  
  26.  
  27.  
  28. If rsd.RecordCount > 0 Then 'checking if there is any record exists in the table. if its found any record then
  29. 'the recordcount should be greater than zero.
  30. rsd.MoveFirst 'pointing 'rsd' to the first record in the 'table. this actually notifies that a in-scanning process
  31. 'through the table is about to begin.
  32. While Not rsd.EOF() 'looping through the table until the end of file is reached
  33. cur_emp = rsd!Firstname 'parsing current cell value of 'empname' to the string var. 'cur_emp'
  34. If UCase(cur_emp) = UCase(e_name) Then 'now checking whether 'cur_emp' and the parameter passed does have
  35. IsEmpExists = True 'same value or not. if yes then employee already exists. so returns
  36. Exit Function 'true. once it becomes true there is no need of scanning again.so
  37. End If 'exists the function.
  38. rsd.MoveNext
  39. Wend
  40. IsEmpExists = False 'if no matching is found between 'cur_emp' and 'e_name' the function returns false,means there
  41. 'is no such employee exists with same name in the table.so you can proceed to add it into the
  42. 'database
  43. Else 'there is no existing record found, so returns false
  44. IsEmpExists = False
  45. End If
  46. End Function
  47.  
  48.  
  49.  
  50. Private Sub Command1_Click()
  51. If Trim(txtempname.Text) <> "" Then
  52. If IsEmpExists(Trim(txtempname.Text)) Then
  53. MsgBox "The employee " & Chr(34) & Trim(txtempname.Text) & Chr(34) & " already exists in the database." & vbCrLf & _
  54. "Please use a different name rather than this one.", vbInformation, "Duplicate Employee"
  55. txtempname.SelStart = 0
  56. txtempname.SelLength = Len(Trim(txtempname.Text))
  57. txtempname.SetFocus
  58. Else
  59. rs.MoveNext
  60. rs.AddNew
  61. rs!Firstname = Trim(txtempname.Text)
  62. rs!StudentNumber = 324324535 'just made this because my primary key is not auto number
  63.  
  64. rs.Update
  65. Call LoadEmployees
  66. MsgBox "New employee created.", vbInformation, "Done"
  67. txtempname.Text = ""
  68. txtempname.SetFocus
  69. End If
  70. Else
  71. MsgBox "The employee name should not be null.", vbCritical
  72. txtempname.SetFocus
  73. End If
  74. End Sub
  75.  
  76. Private Sub Form_Load()
  77.  
  78. ''making connection to the database and opening the employee table to retrieve existing records
  79. Set Conn = New ADODB.Connection
  80. Set rs = New ADODB.Recordset
  81.  
  82. esql = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\MAINDB2.mdb" & ";Persist Security Info=False"
  83.  
  84. Conn.Open (esql), , , 0
  85.  
  86.  
  87. rs.CursorType = adOpenStatic
  88. rs.CursorLocation = adUseClient
  89. rs.LockType = adLockPessimistic
  90. rs.Source = ("Select * From tblStudacct")
  91. rs.ActiveConnection = Conn
  92. rs.Open
  93.  
  94. If rs.RecordCount > 0 Then
  95. rs.MoveFirst
  96. Else
  97. MsgBox "No employee details found." & vbCrLf & _
  98. "'Please add some records.", vbInformation
  99. End If
  100. ''creating the list of existing employees
  101. Call LoadEmployees
  102. End Sub
  103.  
  104.  
  105. Public Sub LoadEmployees()
  106. Dim rsx As New Recordset
  107.  
  108. Set rsx = New ADODB.Recordset
  109. rsx.CursorType = adOpenStatic
  110. rsx.CursorLocation = adUseClient
  111. rsx.LockType = adLockPessimistic
  112. rsx.Source = ("Select * From tblStudacct")
  113. rsx.ActiveConnection = Conn
  114. rsx.Open
  115.  
  116.  
  117. List1.Clear
  118.  
  119. If rsx.RecordCount > 0 Then
  120. rsx.MoveFirst
  121. While Not rsx.EOF()
  122. With List1
  123. .AddItem rsx!Firstname
  124. .ListIndex = .ListCount - 1
  125. End With
  126. rsx.MoveNext
  127. Wend
  128. Exit Sub
  129. End If
  130. List1.AddItem "-=No Existing Employees=-"
  131. End Sub

paste it on a blank form soo you can see that comments,

Last edited by hawisme000; Dec 29th, 2007 at 1:27 am.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Visual Basic 4 / 5 / 6 Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC