473,503 Members | 1,643 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Error Handling

lee123
556 Contributor
hello there i was wondering if anybody know how to do this. I have A form that needs some error handling and I don't know how to do this this form is a lessors form for an apartment building when They add a new lessor in the database i don't want them to enter in the same ID that already exist in the form.. I have One Adodc control on the form and two Buttons one is for them to Add and the other is to Save the information that is tied to a database.

could someone suggest a way to do this in the button i have this code:

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdSave_Click()
  2.     Adodc1.Recordset.MoveNext   
  3. End Sub
how do i put an error handle to say when they add an apartment number and if the apartment number already exist then bring a msgbox up that says "This Apartment Number Already Exist, Add A different Apartment Number In.."

lee123
May 18 '09 #1
1 1215
OuTCasT
374 Contributor
Hey Lee123

You can use a function to check if the apartment number already exists in the database.

Expand|Select|Wrap|Line Numbers
  1. Private Function ValidateApartment(ByVal Apartment As String) As Boolean
  2.         Dim sqlCon As SqlConnection = Nothing
  3.         Dim sqlCom As SqlCommand = Nothing
  4.         Dim lookupID As String
  5.  
  6.         lookupID = Nothing
  7.  
  8.         'Check for ApartmentNumber
  9.         If ((Apartment Is Nothing)) Then
  10.             MsgBox("[ValidateApartment] Input Validation of Apartment Failed", MsgBoxStyle.Critical)
  11.             Return False
  12.         End If
  13.         Try
  14.             sqlCon = New SqlConnection("Server=(local);Data Source=.\SQLEXPRESS;Initial Catalog=DB;Integrated Security=True;Pooling=False")
  15.             sqlCon.Open()
  16.  
  17.             'Create sqlCommand to retrieve Apartment
  18.             sqlCom = New SqlCommand("select [ApartmentNumber] from Apartments where [Apartment]=@ApartmentNumber COLLATE SQL_Latin1_General_CP1_CI_AS", sqlCon)
  19.             sqlCom.Parameters.Add("@ApartmentNumber", SqlDbType.VarChar, 25)
  20.             sqlCom.Parameters("@Apartment").Value = txtApartment.Text
  21.  
  22.             lookupID = sqlCom.ExecuteScalar
  23.  
  24.             sqlCom.Dispose()
  25.             sqlCon.Dispose()
  26.         Catch ex As Exception
  27.             sqlCom.Dispose()
  28.             sqlCon.Dispose()
  29.             MsgBox("Apartment NUmber Already Exists Please make another selection", MsgBoxStyle.Information)
  30.         End Try
  31.         If (lookupID Is Nothing) Then
  32.             Return False
  33.         End If
  34.         Return (String.Compare(lookupID.ToUpper, txtApartment.Text.ToUpper, False) = 0)
  35.     End Function
And then call the function when you leave the textbox.

Expand|Select|Wrap|Line Numbers
  1. If ValidateApartment(txtApartment.Text) = True Then
  2.             MsgBox("Apartment Number already exists..Please choose another Apartment")
  3.  
  4.         Else
  5.             If ValidateApartment(txtApartment.Text) = False Then
  6.                 Exit Sub
  7.             End If
  8.         End If
May 25 '09 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
3255
by: WSeeger | last post by:
When creating a new class, is it encouraged to always include error handling routines within your LET and GET procedures? It's seems that most text books never seem to include much about error...
12
6644
by: Christian Christmann | last post by:
Hi, assert and error handling can be used for similar purposes. When should one use assert instead of try/catch and in which cases the error handling is preferable? I've read somewhere that...
6
8437
by: Squirrel | last post by:
I have a command button on a subform to delete a record. The only statement in the subroutine is: DoCmd.RunCommand acCmdDeleteRecord The subform's recordsource is "select * from tblVisit order...
13
4432
by: Thelma Lubkin | last post by:
I use code extensively; I probably overuse it. But I've been using error trapping very sparingly, and now I've been trapped by that. A form that works for me on the system I'm using, apparently...
21
4372
by: Anthony England | last post by:
Everyone knows that global variables get re-set in an mdb when an un-handled error is encountered, but it seems that this also happens when the variable is defined as private at form-level. So...
3
2836
by: Stefan Johansson | last post by:
Hi all I'am moving from Visual Foxpro and have a question regarding "best practice" error handling in vb .net. In VFP I have always used a "central" error handling object in order to have a...
4
1910
by: Al Williams | last post by:
Hi, I have error handling in place throughout my application. I also start the application wrapped in error handling code to catch any unexpected exceptions (i.e. exceptions that occur where I...
10
2268
by: Anthony England | last post by:
(sorry for the likely repost, but it is still not showing on my news server and after that much typing, I don't want to lose it) I am considering general error handling routines and have...
0
11551
by: Lysander | last post by:
Thought I would give something back with a few articles. This article is a bit of code to add error handling. When I have time, I want to write articles on multilingual databases, and Access...
9
3277
by: MrDeej | last post by:
Hello guys! We have an SQL server which sometimes makes timeouts and connection errors. And we have an function witch writes and updates data in 2 tables on this server. When the SQL server error...
0
7278
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7328
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6991
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7458
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5578
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3167
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1512
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
380
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.