473,394 Members | 1,965 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

On error handling

Hello,

I have a main Case form, that when I click a button, a new Client form opens. On the Client form, I have 3 text boxes that users use to create a new client, first name, last name, and date of birth. I also have 2 invisible text boxes named Client name and ID that I ended up creating due to one of the problems I am encountering. All text boxes are bound from the Clients table, and the client name is just the first and last concatenated. I then have an Add New Client button. On the Clients table, the ID is the primary key and the first, last, and dob fields are all required and indexed to all be unique before the client is added to the table.

What I want, is for after the first, last, and dob text boxes are filled out, when I click Add New button, the client ID that was automated from the entry as well as the concatenated client name are populated into the Main form and the Client form closes.

If they didn't fill out all 3 fields, then a message states that all three fields are required. They then have to enter the information and try again. Then, if they enter all three fields, if all three are not unique, a message states that the client already exists. They should then be able to look at the listbox that is on the same form and choose the client.

I am having trouble with the error handling. I have tried different ways and nothing works. The closest I came to was getting the message to state client exists but the form closed so they couldn't access the listbox then. Here is my code so far with no error handling.
Expand|Select|Wrap|Line Numbers
  1. Private Sub AddNewClient_Click()
  2. If IsNull([First Name]) Or IsNull([Last Name]) Or IsNull(DOB) Then
  3. MsgBox "All fields are required", vbOKOnly, "Error"
  4. End If
  5. If Me.First_Name <> "" And Me.Last_Name <> "" And Me.DOB <> "" Then
  6. Dim ClName As Variant
  7. Dim ClientID As Integer
  8. ClientID = Me.Client_ID.Value
  9. ClName = Me.Client_Name.Value
  10. Forms![Case]![Client ID].Value = ClientID
  11. Forms![Case]![Client].Value = ClName
  12. Forms![Case]![Date Requested].Enabled = True
  13. Forms![Case]![Staff Requesting].Enabled = True
  14. Forms![Case]![Language Requested].Enabled = True
  15. Forms![Case]![Service Requested].Enabled = True
  16. Forms![Case]![Service Date].Enabled = True
  17. Forms![Case]![Outcome].Enabled = True
  18. Forms![Case]![Date Requested].SetFocus
  19. DoCmd.Close acForm, "Client", acSaveYes
  20. End If
  21. End Sub
Any help would be appreciated. Thank you.
Jun 12 '14 #1

✓ answered by twinnyfo

The "Error" code above will never execute unless there is a "code error".

I believe what you are trying to do is force the user to add a new client if the client is not found in the Clients Table. This is not an "Error"--this is a user not doing what you want them to.

To modify my original response, let's take a look at this:

Expand|Select|Wrap|Line Numbers
  1. Private Sub AddNewClient_Click()  
  2. On Error Goto EH:
  3.     Dim ClName As String
  4.     Dim ClientID As Integer
  5.  
  6.     If IsNull([First Name]) Or IsNull([Last Name]) Or IsNull(DOB) Then
  7.         MsgBox "All fields are required", vbOKOnly, "Error"
  8.     Else
  9.         If Nz(DLookup("[ClientID]", "Clients", _
  10.             "[First Name] = '" & Me("First Name") & "'" & _
  11.             " AND [Last Name] = '" & Me("Last Name") & "'" & _
  12.             " AND [DOB] = #" & Me.DOB & "#"), 0) = 0 Then
  13.             'Add the New Client
  14.             ClientID = Me.Client_ID
  15.             ClName = Me.Client_Name
  16.             With Forms![Case]
  17.                 .[Client ID] = ClientID
  18.                 .[Client] = ClName
  19.                 .[Date Requested].Enabled = True
  20.                 .[Staff Requesting].Enabled = True
  21.                 .[Language Requested].Enabled = True
  22.                 .[Service Requested].Enabled = True
  23.                 .[Service Date].Enabled = True
  24.                 .[Outcome].Enabled = True
  25.                 .[Date Requested].SetFocus
  26.             End With
  27.             DoCmd.Close acForm, "Client"
  28.         Else
  29.             'Add Code here to go to the Pre-existing Client
  30.         End if
  31.     End If  
  32.     Exit Sub 
  33. EH: 
  34.     MsgBox Err.Description & vbCrLf & vbCrLf & _
  35.         "Please contact your Database Administrator" 
  36.     Exit Sub 
  37. End Sub
In line 6 above, it checks to see if all fields were entered. If not, the user is told so and the code stops.

In lines 9-12, (since all fields were entered) the code looks to see if the client can be found int he table. The Nz function is there to handle a Null--which means there was no record).

If the value is 0, which is what Nz will return with no record, then the client is added to the Case Form (lines 14-26)

Line 27 closes the Client Form

Line 29, you would have to build... We can help, but I don't know how your forms/tables look to do this adequately. I need try some things and we will work out solutions if you come across problems.

You will also notice in Lines 2, 33-35, I have true "error handling" code.

Please let me know how this code helps out with the tasak at hand and we can move forward with any of your other challenges with this project.

19 1591
twinnyfo
3,653 Expert Mod 2GB
MysticElaine,

It looks as if your If ... Then Statements might be a bit out of sync with what you want to do. If none of the Text Boxes are Null (conditions of the First If...Then), there is no need for the second If...Then.

Expand|Select|Wrap|Line Numbers
  1. Private Sub AddNewClient_Click() 
  2. On Error Goto EH:
  3.     If IsNull([First Name]) Or IsNull([Last Name]) Or IsNull(DOB) Then 
  4.         MsgBox "All fields are required", vbOKOnly, "Error"
  5.     Else
  6.         Dim ClName As Variant 
  7.         Dim ClientID As Integer 
  8.         ClientID = Me.Client_ID.Value 
  9.         ClName = Me.Client_Name.Value 
  10.         Forms![Case]![Client ID].Value = ClientID 
  11.         Forms![Case]![Client].Value = ClName 
  12.         Forms![Case]![Date Requested].Enabled = True 
  13.         Forms![Case]![Staff Requesting].Enabled = True 
  14.         Forms![Case]![Language Requested].Enabled = True 
  15.         Forms![Case]![Service Requested].Enabled = True 
  16.         Forms![Case]![Service Date].Enabled = True 
  17.         Forms![Case]![Outcome].Enabled = True 
  18.         Forms![Case]![Date Requested].SetFocus 
  19.         DoCmd.Close acForm, "Client", acSaveYes 
  20.     End If 
  21.     Exit Sub
  22. EH:
  23.     MsgBox Err.Description
  24.     Exit Sub
  25. End Sub
However, this does not address your desire for the code to look for a pre-existing customer. You could easily do this through either a recordset or through a DLookup function. Without knowing what your Table names are it is difficult to provide detailed assistance.

This may be a start to get you going in the right direction. I'll be glad to provide additional assistance if you need it.
Jun 12 '14 #2
Hi, thank you for the response. Yes, I would love some additional assistance. I am very new to Access and coding so have been teaching myself as I go along. As it stands right now, if a user enters identical information, the Client form closes and populates the Client ID and Client fields of the main Case form. However, a new Client ID is given, which is bad since the client already exists and has a different ID number. This is where I am having trouble. I thought I could add an Error handler, but all that did was give the message that the client already exists and still closed the Client form and populated the Case form. So I thought maybe I needed to move the DoCmd.Close somewhere else, but I didn't know where.

Right now, I have a "Case" form that is linked with the "Cases" table. I also have a "Client" form that is linked with the "Clients" table. On the Case form, they click a New Entry button that pulls up the Client form. The user is supposed to search first for the client and choose from the listbox if they exist (left side of the form) before adding a new client (right side of form). I did make it so that they had to type something in one of the search boxes before being able to add anyone, but that doesn't mean they won't just type one letter and then try to add the client.

For the Client form, I have the "First Name", "Last Name", and "DOB" fields visible and are all indexed to be unique before being added to the Clients table. I also have the "Client ID", which is the primary key, and "Client Name" fields invisible so that when a new unique client is added, those fields are populated into the Case form's "Client ID" and "Client" fields upon the Client form closing.

Sorry for being so long-winded and thanks for the help.

Edit: Can I do
Expand|Select|Wrap|Line Numbers
  1. If DLookup("[First Name]","Clients","[First Name]=Clients.[First Name]Then ")
?
Jun 12 '14 #3
twinnyfo
3,653 Expert Mod 2GB
OK. A lot going on! Let's try to take this one step at a time.

First things first: Although it is EXTREMELY rare that two people with the same First and Last Names also have the same DOB, that is kinda immaterial. I would highly recommend that in your Clients Table that there simply be a unique identifier for all clients. After all, someone could be looking for "John Smith, 14 Aug 04" and really mean "John Smyth 14 Aug 04". Try to find another way to identify your clients. Then, when you select a client, it is always based on the ClientID (even though the criteria for looking for the Client is based on name and DOB).

Second, if you want the User to look for a client before they can enter a new client, then build your form that way. You can do this by using unbound text box controls in which the user enters data (even partial data) and the db will look for records that match those criteria. Your users should never "accidentally" create a record if you don't want them to, which is why you are coming up with new client IDs.

I know you are asking a lot, and I am not giving a lot, but I promise you, if we work through this piece by piece, we will eventually get there.

What is the first step you want to tackle, because right now, it seems like there is too much for one post.

I'm here to hepp!

:-)
Jun 12 '14 #4
I do have a client ID that is the primary key and is auto number. Also, the only information we can put down to identify the client is first, last, and dob (MM/DD only), which is why all 3 fields must be unique before being added to the clients table.

I already have the form able to type in different text boxes and filter a list box to identify an existing client and then, once chosen, it populates the main form with the info.

My problem is the scenario in which a lazy person decides not to first search for the client and go straight to adding them. They type one letter in the search text box to now enable the text boxes that allow a new client to be added. They then type in the first, last, and dob. They click Add New button and the main form populates with the information. The problem is, even though the main form populates, it populates with the wrong ID number for that client, who already has a unique ID. So I need to be able to let them know that the user already exists and have the client form stay open so that they can then search the list box for the client.

Thanks

P.S. Is there a way I can attach my project here?

Edit: Here is something else I was trying with the recordset but it errors
Expand|Select|Wrap|Line Numbers
  1. Private Sub AddNewClient_Click()
  2. If IsNull([First Name]) Or IsNull([Last Name]) Or IsNull(DOB) Then
  3. MsgBox "All fields are required", vbOKOnly, "Error"
  4. Else
  5.     Dim dbs As Database
  6.     Dim rst As DAO.Recordset
  7.     Dim str As String
  8.     Set dbs = CurrentDb()
  9.     Set rst = dbs.OpenRecordset("Clients", dbOpenDynaset)
  10.     rst.FindFirst "[First Name] = Me.[First Name] AND [Last Name] = Me.[Last Name] AND [DOB] = Me.[DOB]"
  11.     If Not rst.NoMatch Then
  12.     MsgBox "Client exists"
  13.     GoTo Cleanup:
  14.     Else
  15. If Me.First_Name <> "" And Me.Last_Name <> "" And Me.DOB <> "" Then
  16. Dim ClName As Variant
  17. Dim ClientID As Integer
  18. ClientID = Me.Client_ID.Value
  19. ClName = Me.Client_Name.Value
  20. Forms![Case]![Client ID].Value = ClientID
  21. Forms![Case]![Client].Value = ClName
  22. Forms![Case]![Date Requested].Enabled = True
  23. Forms![Case]![Staff Requesting].Enabled = True
  24. Forms![Case]![Language Requested].Enabled = True
  25. Forms![Case]![Service Requested].Enabled = True
  26. Forms![Case]![Service Date].Enabled = True
  27. Forms![Case]![Outcome].Enabled = True
  28. Forms![Case]![Date Requested].SetFocus
  29. DoCmd.Close acForm, "Client", acSaveYes
  30. End If
  31. End If
  32. End If
  33. Cleanup:
  34. rst.Close
  35. Set rst = Nothing
  36. Set dbs = Nothing
  37. End Sub
  38.  
  39.  
It says for the rst.FindFirst that the Microsoft Access database engine does not recognize 'Me.First Name' as a valid field name or expression
Jun 12 '14 #5
twinnyfo
3,653 Expert Mod 2GB
Elaine,

Let's go one step at a time. First, the Client entry form. First, make this a completely unbound form--not attached or associated with ANY tables or queries. When the User types in a Name, there will be no Client ID generated, because it is not a record--it's just data.

When the user finishes adding the Last, First and DOB, they click "Add" or "Search"--either one should work for this scenario. The code behind the button will do the following:
  1. Search the Current Records for the Client
  2. If the Client Exists, go to that record on the Main Form
  3. If the Client does not exist, create a new record
Let's start with this functionality first, because I think this is at the heart of your orignal post. Once we get that working, we can address some of the other issues.

BTW, it is possible to post your projec there, but you have to zip it. However, at my work PC, I cannot access it, so I have to "fly blind" unlike many of hte other users here.
Jun 13 '14 #6
I originally had the form have 3 text boxes. As they typed, the listbox containing all the clients would filter and if the client didn't show up, then when they hit the button, the client would be added. However, my supervisor didn't like that idea and thought it was too complicated for people to understand. So he requested that the form have two sides. On the left side, were the search boxes that filtered the listbox. The user selects the client in the listbox if it exists and hits a button that populates the main form. The right side contains the textboxes that allow the user to add a new client, which not only updates the table with a new record, but also populates the main form with the information.

Unfortunately, I can't change any of the forms layouts anymore as someone else is now taking the project to beautify the forms and create all the necessary queries and reports functions for all the different requests that might come up. We have to have it presentable by next Friday. I told them I would work on the On Errors, which I didn't think there would be many to do.

I have attached my project also. I realize you can't access it at work, but unfortuately, I think it needs to be seen to understand what I am doing. I will also paste my coding for all three forms, in case that will help, but I realize that is a lot to look at. Thanks again.

.................................................. .................................................. ..............
*****Read below for coding of each form if not looking at the attachment*****************

Main Form "Case":
Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. Private Sub Client_GotFocus()
  5. DoCmd.OpenForm "Client", acNormal, , , acFormAdd
  6. End Sub
  7.  
  8.  
  9. Private Sub Cost_AfterUpdate()
  10. If [Cost] > 0 Then
  11.     [Providers].Enabled = False
  12. Else
  13.     [Providers].Enabled = True
  14. End If
  15. End Sub
  16.  
  17. Private Sub Form_Load()
  18.     [Case ID].Enabled = False
  19.     [Client ID].Enabled = False
  20.     [Client].Enabled = False
  21.     [Date Requested].Enabled = False
  22.     [Staff Requesting].Enabled = False
  23.     [Language Requested].Enabled = False
  24.     [Service Requested].Enabled = False
  25.     [Service Date].Enabled = False
  26.     [Outcome].Enabled = False
  27.     [Date Provided].Enabled = False
  28.     [Providers].Enabled = False
  29.     [Number of Pages].Enabled = False
  30.     [Time].Enabled = False
  31.     [Cost].Enabled = False
  32.     [Comments].Enabled = False
  33. End Sub
  34.  
  35. Private Sub New_Entry_Click()
  36. DoCmd.OpenForm "Client", acNormal, , , acFormAdd
  37. End Sub
  38.  
  39. Private Sub Outcome_Change()
  40. If [Outcome] = "Pending" Then
  41.     [Date Provided].Enabled = False
  42.     [Date Provided] = Null
  43.     [Providers].Enabled = False
  44.     [Providers] = Null
  45.     [Number of Pages].Enabled = False
  46.     [Number of Pages] = Null
  47.     [Time].Enabled = False
  48.     [Time] = Null
  49.     [Cost].Enabled = False
  50.     [Cost] = Null
  51.     [Comments].Enabled = True
  52. Else
  53.         If [Outcome] = "Interpretation" Then
  54.             [Date Provided].Enabled = True
  55.             [Providers].Enabled = True
  56.             [Number of Pages].Enabled = False
  57.             [Number of Pages] = Null
  58.             [Time].Enabled = True
  59.             [Comments].Enabled = True
  60.         Else
  61.                 If [Outcome] = "Translation" Then
  62.                     [Date Provided].Enabled = True
  63.                     [Providers].Enabled = True
  64.                     [Number of Pages].Enabled = True
  65.                     [Time].Enabled = True
  66.                     [Comments].Enabled = True
  67.                 Else
  68.                     [Date Provided].Enabled = False
  69.                     [Date Provided] = Null
  70.                     [Providers].Enabled = False
  71.                     [Providers] = Null
  72.                     [Number of Pages].Enabled = False
  73.                     [Number of Pages] = Null
  74.                     [Time].Enabled = False
  75.                     [Time] = Null
  76.                     [Cost].Enabled = False
  77.                     [Cost] = Null
  78.                     [Comments].Enabled = True
  79.                 End If
  80.         End If
  81. End If
  82.  
  83. End Sub
  84.  
  85. Private Sub Providers_AfterUpdate()
  86. If [Providers] = "Outsider" Then
  87.     [Cost].Enabled = True
  88. Else
  89.     [Cost].Enabled = False
  90.     [Cost] = Null
  91. End If
  92. End Sub
  93.  
  94. Private Sub Providers_GotFocus()
  95. If [Outcome] = "Translation" Then
  96. Dim TranSQL As String
  97. TranSQL = "SELECT DISTINCT Providers.[Staff Name], Providers.Translation"
  98. TranSQL = TranSQL & " FROM Providers"
  99. TranSQL = TranSQL & " WHERE (((Providers.Translation)=True))"
  100. TranSQL = TranSQL & " ORDER BY Providers.[Staff Name]"
  101.  
  102. Me![Providers].RowSourceType = "Table/Query"
  103. Me![Providers].RowSource = TranSQL
  104. End If
  105.  
  106. If [Outcome] = "Interpretation" Then
  107. Dim IntSQL As String
  108. IntSQL = "SELECT DISTINCT Providers.[Staff Name], Providers.Interpretation"
  109. IntSQL = IntSQL & " FROM Providers"
  110. IntSQL = IntSQL & " WHERE (((Providers.Interpretation)=True))"
  111. IntSQL = IntSQL & " ORDER BY Providers.[Staff Name]"
  112. Me![Providers].RowSourceType = "Table/Query"
  113. Me![Providers].RowSource = IntSQL
  114. End If
  115. End Sub
  116.  
  117. Private Sub Search_Case_Click()
  118. DoCmd.OpenForm "SearchCases", acNormal
  119. End Sub
  120.  
From the main form, they can choose to search an existing case, which opens the "SearchCases" form
SearchCases form:
Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3. Dim CaseSearchQry As String
  4.  
  5. Private Sub Form_Load()
  6. CaseSearchQry = "SELECT Cases.[Case ID], Cases.Client, Cases.[Language Requested], Cases.[Service Requested], Cases.[Service Date], Cases.Outcome"
  7. CaseSearchQry = CaseSearchQry & " FROM Cases WHERE (((Cases.Outcome) = 'Pending'))"
  8. CaseSearchQry = CaseSearchQry & " ORDER BY Cases.Client, Cases.[Language Requested], Cases.[Service Requested], Cases.[Service Date]"
  9. Me.CasesList.RowSource = CaseSearchQry
  10. Me.CasesList.Requery
  11. End Sub
  12.  
  13. Private Sub UpdateListboxClient()
  14. CaseSearchQry = "SELECT Cases.[Case ID], Cases.Client, Cases.[Language Requested], Cases.[Service Requested], Cases.[Service Date], Cases.Outcome FROM Cases"
  15. CaseSearchQry = CaseSearchQry & " WHERE (Cases.Outcome = 'Pending'"
  16. CaseSearchQry = CaseSearchQry & " AND Cases.Client Like " & Chr(34) & "*" & Me.SearchName.Text & "*" & Chr(34)
  17. CaseSearchQry = CaseSearchQry & " AND Cases.[Language Requested] Like " & Chr(34) & Me.SearchLanguage & "*" & Chr(34)
  18. CaseSearchQry = CaseSearchQry & " AND Cases.[Service Requested] Like " & Chr(34) & Me.SearchService & "*" & Chr(34)
  19. CaseSearchQry = CaseSearchQry & " AND Cases.[Service Date] Like " & Chr(34) & Me.SearchDate & "*" & Chr(34) & ")"
  20. CaseSearchQry = CaseSearchQry & " ORDER BY Cases.Client, Cases.[Language Requested], Cases.[Service Requested], Cases.[Service Date]"
  21. Me.CasesList.RowSource = CaseSearchQry
  22. Me.CasesList.Requery
  23. End Sub
  24.  
  25. Private Sub UpdateListboxLanguage()
  26. CaseSearchQry = "SELECT Cases.[Case ID], Cases.Client, Cases.[Language Requested], Cases.[Service Requested], Cases.[Service Date], Cases.Outcome FROM Cases"
  27. CaseSearchQry = CaseSearchQry & " WHERE (Cases.Outcome = 'Pending'"
  28. CaseSearchQry = CaseSearchQry & " AND Cases.Client Like " & Chr(34) & "*" & Me.SearchName & "*" & Chr(34)
  29. CaseSearchQry = CaseSearchQry & " AND Cases.[Language Requested] Like " & Chr(34) & Me.SearchLanguage.Text & "*" & Chr(34)
  30. CaseSearchQry = CaseSearchQry & " AND Cases.[Service Requested] Like " & Chr(34) & Me.SearchService & "*" & Chr(34)
  31. CaseSearchQry = CaseSearchQry & " AND Cases.[Service Date] Like " & Chr(34) & Me.SearchDate & "*" & Chr(34) & ")"
  32. CaseSearchQry = CaseSearchQry & " ORDER BY Cases.Client, Cases.[Language Requested], Cases.[Service Requested], Cases.[Service Date]"
  33. Me.CasesList.RowSource = CaseSearchQry
  34. Me.CasesList.Requery
  35. End Sub
  36.  
  37. Private Sub UpdateListboxService()
  38. CaseSearchQry = "SELECT Cases.[Case ID], Cases.Client, Cases.[Language Requested], Cases.[Service Requested], Cases.[Service Date], Cases.Outcome FROM Cases"
  39. CaseSearchQry = CaseSearchQry & " WHERE (Cases.Outcome = 'Pending'"
  40. CaseSearchQry = CaseSearchQry & " AND Cases.Client Like " & Chr(34) & "*" & Me.SearchName & "*" & Chr(34)
  41. CaseSearchQry = CaseSearchQry & " AND Cases.[Language Requested] Like " & Chr(34) & Me.SearchLanguage & "*" & Chr(34)
  42. CaseSearchQry = CaseSearchQry & " AND Cases.[Service Requested] Like " & Chr(34) & Me.SearchService.Text & "*" & Chr(34)
  43. CaseSearchQry = CaseSearchQry & " AND Cases.[Service Date] Like " & Chr(34) & Me.SearchDate & "*" & Chr(34) & ")"
  44. CaseSearchQry = CaseSearchQry & " ORDER BY Cases.Client, Cases.[Language Requested], Cases.[Service Requested], Cases.[Service Date]"
  45. Me.CasesList.RowSource = CaseSearchQry
  46. Me.CasesList.Requery
  47. End Sub
  48.  
  49. Private Sub UpdateListboxDate()
  50. CaseSearchQry = "SELECT Cases.[Case ID], Cases.Client, Cases.[Language Requested], Cases.[Service Requested], Cases.[Service Date], Cases.Outcome FROM Cases"
  51. CaseSearchQry = CaseSearchQry & " WHERE (Cases.Outcome = 'Pending'"
  52. CaseSearchQry = CaseSearchQry & " AND Cases.Client Like " & Chr(34) & "*" & Me.SearchName & "*" & Chr(34)
  53. CaseSearchQry = CaseSearchQry & " AND Cases.[Language Requested] Like " & Chr(34) & Me.SearchLanguage & "*" & Chr(34)
  54. CaseSearchQry = CaseSearchQry & " AND Cases.[Service Requested] Like " & Chr(34) & Me.SearchService & "*" & Chr(34)
  55. CaseSearchQry = CaseSearchQry & " AND Cases.[Service Date] Like " & Chr(34) & Me.SearchDate.Text & "*" & Chr(34) & ")"
  56. CaseSearchQry = CaseSearchQry & " ORDER BY Cases.Client, Cases.[Language Requested], Cases.[Service Requested], Cases.[Service Date]"
  57. Me.CasesList.RowSource = CaseSearchQry
  58. Me.CasesList.Requery
  59. End Sub
  60.  
  61. Private Sub SearchDate_Change()
  62. Call UpdateListboxDate
  63. End Sub
  64.  
  65. Private Sub SearchLanguage_Change()
  66. Call UpdateListboxLanguage
  67. End Sub
  68.  
  69. Private Sub SearchName_Change()
  70. Call UpdateListboxClient
  71. End Sub
  72.  
  73. Private Sub EditCase_Click()
  74. DoCmd.OpenForm "Case", acNormal, , "[Case ID]=" & CasesList, acFormEdit, acNormal
  75. DoCmd.Close acForm, "SearchCases", acSaveNo
  76. Forms![Case]![Date Requested].Enabled = False
  77. Forms![Case]![Staff Requesting].Enabled = False
  78. Forms![Case]![Language Requested].Enabled = False
  79. Forms![Case]![Service Requested].Enabled = False
  80. Forms![Case]![Service Date].Enabled = False
  81. Forms![Case]![Outcome].Enabled = True
  82. Forms![Case]![Comments].Enabled = True
  83. End Sub
  84.  
  85. Private Sub SearchService_Change()
  86. Call UpdateListboxService
  87. End Sub
  88.  
From the main form, they can also choose to add a new entry. This opens the "Client" form so that they can either choose an existing client or add a new one.
Client form:
Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3. Dim ClientSearchQry As String
  4.  
  5. Private Sub UpdateListboxFN()
  6. ClientSearchQry = "SELECT Clients.[Client ID], Clients.[First Name], Clients.[Last Name], Clients.DOB, Clients.[Client Name]"
  7. ClientSearchQry = ClientSearchQry & " FROM Clients"
  8. ClientSearchQry = ClientSearchQry & " WHERE (Clients.[First Name] Like '" & Me.[Search First Name].Text & "*' AND Clients.[Last Name] Like " & Chr(34) & Me.[Search Last Name] & "*" & Chr(34) & ")"
  9. ClientSearchQry = ClientSearchQry & " ORDER BY Clients.[First Name], Clients.[Last Name], Clients.[DOB]"
  10. Me.ClientList.RowSource = ClientSearchQry
  11. Me.ClientList.Requery
  12. End Sub
  13. Private Sub UpdateListboxLN()
  14. ClientSearchQry = "SELECT Clients.[Client ID], Clients.[First Name], Clients.[Last Name], Clients.DOB, Clients.[Client Name]"
  15. ClientSearchQry = ClientSearchQry & " FROM Clients"
  16. ClientSearchQry = ClientSearchQry & " WHERE (Clients.[First Name] Like '" & Me.[Search First Name] & "*' AND Clients.[Last Name] Like " & Chr(34) & Me.[Search Last Name].Text & "*" & Chr(34) & ")"
  17. ClientSearchQry = ClientSearchQry & " ORDER BY Clients.[First Name], Clients.[Last Name], Clients.[DOB]"
  18. Me.ClientList.RowSource = ClientSearchQry
  19. Me.ClientList.Requery
  20. End Sub
  21.  
  22. Private Sub AddNewClient_Click()
  23.  
  24. If IsNull([First Name]) Or IsNull([Last Name]) Or IsNull(DOB) Then
  25. MsgBox "All fields are required", vbOKOnly, "Error"
  26. Else
  27.     Dim dbs As Database
  28.     Dim rst As DAO.Recordset
  29.     Dim str As String
  30.     Set dbs = CurrentDb()
  31.     Set rst = dbs.OpenRecordset("Clients", dbOpenDynaset)
  32.     rst.FindFirst "[First Name] = Me.[First Name] AND [Last Name] = Me.[Last Name] AND [DOB] = Me.[DOB]"
  33.     If Not rst.NoMatch Then
  34.     MsgBox "Client exists"
  35.     GoTo Cleanup:
  36.     Else
  37. If Me.First_Name <> "" And Me.Last_Name <> "" And Me.DOB <> "" Then
  38. Dim ClName As Variant
  39. Dim ClientID As Integer
  40. ClientID = Me.Client_ID.Value
  41. ClName = Me.Client_Name.Value
  42. Forms![Case]![Client ID].Value = ClientID
  43. Forms![Case]![Client].Value = ClName
  44. Forms![Case]![Date Requested].Enabled = True
  45. Forms![Case]![Staff Requesting].Enabled = True
  46. Forms![Case]![Language Requested].Enabled = True
  47. Forms![Case]![Service Requested].Enabled = True
  48. Forms![Case]![Service Date].Enabled = True
  49. Forms![Case]![Outcome].Enabled = True
  50. Forms![Case]![Date Requested].SetFocus
  51. DoCmd.Close acForm, "Client", acSaveYes
  52. End If
  53. End If
  54. End If
  55. Cleanup:
  56. rst.Close
  57. Set rst = Nothing
  58. Set dbs = Nothing
  59. End Sub
  60.  
  61.  
  62. Private Sub Form_Load()
  63. ClientSearchQry = "SELECT Clients.[Client ID], Clients.[First Name], Clients.[Last Name], Clients.DOB, Clients.[Client Name]"
  64. ClientSearchQry = ClientSearchQry & " FROM Clients ORDER BY Clients.[First Name], Clients.[Last Name], Clients.[DOB]"
  65. Me.ClientList.RowSource = ClientSearchQry
  66. Me.ClientList.Requery
  67.  
  68. [First Name].Enabled = False
  69. [Last Name].Enabled = False
  70. [DOB].Enabled = False
  71. End Sub
  72.  
  73. Private Sub Search_First_Name_Change()
  74. Call UpdateListboxFN
  75.  
  76. If [Search First Name] = "" Then
  77. [First Name].Enabled = False
  78. [Last Name].Enabled = False
  79. [DOB].Enabled = False
  80. Else
  81. [First Name].Enabled = True
  82. [Last Name].Enabled = True
  83. [DOB].Enabled = True
  84. End If
  85.  
  86. End Sub
  87.  
  88. Private Sub Search_Last_Name_Change()
  89. Call UpdateListboxLN
  90.  
  91. If [Search Last Name] = "" Then
  92. [First Name].Enabled = False
  93. [Last Name].Enabled = False
  94. [DOB].Enabled = False
  95. Else
  96. [First Name].Enabled = True
  97. [Last Name].Enabled = True
  98. [DOB].Enabled = True
  99. End If
  100.  
  101. End Sub
  102.  
  103. Private Sub UseExistingClient_Click()
  104. Dim ClListID As Integer
  105. Dim CLN As Variant
  106. ClListID = Me.ClientList.Column(0)
  107. CLN = Me.ClientList.Column(4)
  108. DoCmd.Close acForm, "Client", acSaveNo
  109. Forms![Case]![Client ID].Value = ClListID
  110. Forms![Case]![Client].Value = CLN
  111. Forms![Case]![Date Requested].Enabled = True
  112. Forms![Case]![Staff Requesting].Enabled = True
  113. Forms![Case]![Language Requested].Enabled = True
  114. Forms![Case]![Service Requested].Enabled = True
  115. Forms![Case]![Service Date].Enabled = True
  116. Forms![Case]![Outcome].Enabled = True
  117. Forms![Case]![Date Requested].SetFocus
  118. End Sub
  119.  
Attached Files
File Type: zip LAP2.zip (203.8 KB, 60 views)
Jun 13 '14 #7
twinnyfo
3,653 Expert Mod 2GB
Here is a little hepp:

Line 32 in the Clients Form above:

Expand|Select|Wrap|Line Numbers
  1. rst.FindFirst "[First Name] = '" & Me.[First Name] & _
  2.     "' AND [Last Name] = '" & Me.[Last Name] & _
  3.     "' AND [DOB] = #" & Me.[DOB] & "#"
I also always recommend keeping your lines of code to less than 80 characters if possible by using the Line continuation marks "& _", as this keeps your code nice and tidy. This also makes creating long string like you have in your Cases and SearchCases Forms. Both methods work, but this saves you some typing.

Are you receiveing erros elsewhere?

Also, the title of this posting is "On Error Handling" and I have noticed that you have no error handling included in any of your code. Are you talking about "errors" or are you talking about preventing a user from doing something they ought not to do?

I also think your number one challenge is that you say this: "Unfortunately, I can't change any of the forms layouts anymore as someone else is now taking the project to beautify the forms and create all the necessary queries and reports functions for all the different requests that might come up." If you can't change the form layouts, then you can't control how the forms get used. In my databases, I shut down everything that I possibly can, so the user can only do what they are supposed to do. If someone doesn't want to follow the rules and look for a pre-existing client first, and we don't shackle them into doing so (via code or forms or whatever), they are not going to do it.

I hope my little bit of code hepped a little bit...
Jun 13 '14 #8
Thanks for the information about the line continuation marks and the use of the # for the date, I forgot about that.

In regards to the On Error tile, in my first post, I had put the code that I wanted the On Error to work on, but I didn't know where to put it as all my attempts had failed. I had gotten the message box in the handler to work, but the form would still close instead of staying open so the user could fix the problem. Right now, Access just automatically closes the form and states it couldn't be saved as it already exists. I wanted to prevent the user from having to start all over by just creating the On Error to trap the error, keep the form open, and let the user choose the client from the listbox instead.

I just read this morning about CancelEvent, so I might try that to keep the form from closing.

Thanks for the info!
Jun 13 '14 #9
For example, this is what I have for the On Error, but the form still closes:
Expand|Select|Wrap|Line Numbers
  1. Private Sub AddNewClient_Click()
  2. On Error GoTo AddNewErr
  3. If Me.First_Name <> "" And Me.Last_Name <> "" And Me.DOB <> "" Then
  4. Dim ClName As Variant
  5. Dim ClientID As Integer
  6. ClientID = Me.Client_ID.Value
  7. ClName = Me.Client_Name.Value
  8. Forms![Case]![Client ID].Value = ClientID
  9. Forms![Case]![Client].Value = ClName
  10. Forms![Case]![Date Requested].Enabled = True
  11. Forms![Case]![Staff Requesting].Enabled = True
  12. Forms![Case]![Language Requested].Enabled = True
  13. Forms![Case]![Service Requested].Enabled = True
  14. Forms![Case]![Service Date].Enabled = True
  15. Forms![Case]![Outcome].Enabled = True
  16. Forms![Case]![Date Requested].SetFocus
  17. DoCmd.Close acForm, "Client", acSaveYes
  18. End If
  19. AddNewErr:
  20. If IsNull([First Name]) Or IsNull([Last Name]) Or IsNull(DOB) Then
  21. MsgBox "All fields are required", vbOKOnly, "Error"
  22. Else
  23. MsgBox "Client Exists", vbOKOnly, "Error"
  24. End If
  25. DoCmd.CancelEvent
  26. End Sub
Jun 13 '14 #10
Luk3r
300 256MB
This is really a shot in the dark, and I hate to interrupt the conversation this far.. but when you trap errors, don't you have to Resume Next when an error is trapped? So in your AddNewErr: section, where you're actually handling the error, you also need "Resume Next" at the end so that the program knows to keep going after trapping an error.

Edit**: I don't know much about VB4/5/6, so this is really a question for learning on my part in the instance that I'm wrong. I would "Resume" in VB.NET and that's why I brought it up here. Always willing to learn! :)
Jun 13 '14 #11
twinnyfo
3,653 Expert Mod 2GB
The "Error" code above will never execute unless there is a "code error".

I believe what you are trying to do is force the user to add a new client if the client is not found in the Clients Table. This is not an "Error"--this is a user not doing what you want them to.

To modify my original response, let's take a look at this:

Expand|Select|Wrap|Line Numbers
  1. Private Sub AddNewClient_Click()  
  2. On Error Goto EH:
  3.     Dim ClName As String
  4.     Dim ClientID As Integer
  5.  
  6.     If IsNull([First Name]) Or IsNull([Last Name]) Or IsNull(DOB) Then
  7.         MsgBox "All fields are required", vbOKOnly, "Error"
  8.     Else
  9.         If Nz(DLookup("[ClientID]", "Clients", _
  10.             "[First Name] = '" & Me("First Name") & "'" & _
  11.             " AND [Last Name] = '" & Me("Last Name") & "'" & _
  12.             " AND [DOB] = #" & Me.DOB & "#"), 0) = 0 Then
  13.             'Add the New Client
  14.             ClientID = Me.Client_ID
  15.             ClName = Me.Client_Name
  16.             With Forms![Case]
  17.                 .[Client ID] = ClientID
  18.                 .[Client] = ClName
  19.                 .[Date Requested].Enabled = True
  20.                 .[Staff Requesting].Enabled = True
  21.                 .[Language Requested].Enabled = True
  22.                 .[Service Requested].Enabled = True
  23.                 .[Service Date].Enabled = True
  24.                 .[Outcome].Enabled = True
  25.                 .[Date Requested].SetFocus
  26.             End With
  27.             DoCmd.Close acForm, "Client"
  28.         Else
  29.             'Add Code here to go to the Pre-existing Client
  30.         End if
  31.     End If  
  32.     Exit Sub 
  33. EH: 
  34.     MsgBox Err.Description & vbCrLf & vbCrLf & _
  35.         "Please contact your Database Administrator" 
  36.     Exit Sub 
  37. End Sub
In line 6 above, it checks to see if all fields were entered. If not, the user is told so and the code stops.

In lines 9-12, (since all fields were entered) the code looks to see if the client can be found int he table. The Nz function is there to handle a Null--which means there was no record).

If the value is 0, which is what Nz will return with no record, then the client is added to the Case Form (lines 14-26)

Line 27 closes the Client Form

Line 29, you would have to build... We can help, but I don't know how your forms/tables look to do this adequately. I need try some things and we will work out solutions if you come across problems.

You will also notice in Lines 2, 33-35, I have true "error handling" code.

Please let me know how this code helps out with the tasak at hand and we can move forward with any of your other challenges with this project.
Jun 13 '14 #12
twinnyfo
3,653 Expert Mod 2GB
Luk3r,

In response to your question, resuming is not necessary. For example if you have code that causes an error, but the continuing code is not dependent upon the previous code, then you may want to Resume Next. However, if your following code is dependent upon the previous, you will want to exit the sub.

Here is an example. Let's say you have several picture objects on your form and the code loads a picture from a file and these file names are all hard coded into the VBA. During the execution, the VBA can't find one of the file names, so this causes an error. Just because one file is missing doesn't mean all the files are missing, so you would want to code to continue.

Another example. Let's say that you are trying to find the value of Sales for the Month of May. Based on that value, you need to calculate thirty other values. If your code causes an error determining the value of Sales in the Month of May, since that variable will be unusable, you want the code to exit.

In either example above, it is possible for the VBA coder to determine what the error is and inform the user exactly what went wrong, so it can be fixed (e.g. tell the user the file name could not be found or that it couldn't find any sales for the month of May)

From a training and learning perspective, here is my advice: Whenever you have your code looking for or calculating a value for a variable or "something" that you are going to use, determine what you are going to do if there is a Null value (the Nz() Function above). If you do a DLookup and it does not find anything that meets the criteria, you will have a Null, which is sometimes difficult to use. However, a zero is easy to use. So, if you look for a zero (Line 12 above) and don't find it, then you have a valid DLookup value.

The point here is simple: I put error handling into all my subs and they very seldom fire during execution, because I try to cover as many possibilities as I can with the Code itself. If I get invlaid data, then I handle it without any real "errors". The code will work around it or tell the User they they entered invalid data.

However, the DBA MUST include Erorr handling, because if not, when ther eis a code error, the DB freezes or closes or does really bad things. Your job, as a coder, is to prevent this from happening!

If you see from my post #12 above, it really does seem like the language of "error handling" has two meanings here. It appears the OP wants to redirect users when they do something incorrectly. Error handling in the code tells the code what to do when the code does something wrong.

I hope this hepped to explain things a bit better for you.
Jun 13 '14 #13
Luk3r
300 256MB
That was a fantastic explanation and I thank you. Reading scenarios the way you wrote them makes much more sense now :)
Jun 13 '14 #14
Hi, thanks. In theory, that is what I want to do. However, it is having the same issues as my original code in that it isn't seeing that the person already exists in the table. It still populated the "Case" form with a new Client ID, not the Client ID the person already has.

I also don't know what you mean by true "error handling". If all three fields are not entered, it produces the error code 3314 (in which I wanted a msgbox to say "all fields required"), and if all three fields are entered but the person already exists in the table, it produces the code 3022 (which I wanted a msgbox to say "client exists" and the form to not close). At one point in time I tried a Select Case, but it didn't seem to work, though I could have done it wrong.

Thanks.

Edit: I just saw your post to Luk3r and saw how you described "Error Handling"
Jun 13 '14 #15
twinnyfo
3,653 Expert Mod 2GB
Well, it appears that Lines 9-26 are trying to add a new client, even if there is an exisiting client.

So, from a trouble shooter's perspective, we have to figure out why it it doing that. Let's add a few lines of code to help us out here (you can always delete this code later on).

Add this code between lines 8 and 9:

Expand|Select|Wrap|Line Numbers
  1.         Dim strFirstTest As String
  2.         Dim strLastTest As String
  3.         Dim dtDOBTest as Date
  4.         strFirstTest = Me("First Name")
  5.         strLastTest = Me("Last Name")
  6.         dtDOBTest = Me.DOB
  7.         MsgBox "Here are your values:" & vbCrLf & vbCrLf & _
  8.             "First:  " & strFirstTest & vbCrLf & _
  9.             "Last:  " & strLastTest & vbCrLf & _
  10.             "DOB:  " & dtDOBTest, vbOkOnly
  11.         Exit Sub
Now, when you run the code, what the the pop up tell you the values are and, more importantly, how do they differ from the values input into the form?

I am causing the code to exit before any records are added, because we just want to test the values the code comes up with.

Yes, we are taking this truly at one step at a time.
Jun 13 '14 #16
Ok, I did that and the message box popped up with all the correct values with the exception that the DOB added a year. My table has it as a text value since we can only type MM/DD.
Jun 13 '14 #17
So I took out the # and replaced with ' in the code and it works!!!

*happy dance*
Jun 13 '14 #18
Thank you so much!!!!! I appreciate your time and patience with me and for getting me through this problem!

*Gives you lots of baked goodies*
Jun 13 '14 #19
twinnyfo
3,653 Expert Mod 2GB
I am so glad that I could help. I know it seemed to take a long time, but at least now you can move forward!

You know, I am especially fond of chocolate chip cookies! ;-)
Jun 13 '14 #20

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

Similar topics

2
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
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...
21
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
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
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...
1
by: Metal Dave | last post by:
I do not understand the error handling of SQL Server here. Any error in bulk insert seems to halt the current T-SQL statement entirely, rendering it impossible to log an error. The first statement...
10
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...
1
by: pob | last post by:
>From a form I have some code that calls 4 modules frmMain 1 mod 2 mod 3 mod 4 mod If mod 1 experiences an error the error handling works fine within mod 1 and writes out the error to a...
0
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...
2
by: Omar Abid | last post by:
Reason of this project: Error handling is one of the most difficult thing that may afford a programmer. It isn't as easy as you think and handling errors in a program some time can make errors...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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
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...

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.