473,403 Members | 2,323 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,403 software developers and data experts.

Need a msgbox to display when a record is not found

Hi I need help displaying a msgbox when office and/or fiscal year do no match the input and therefore cannot be found. Right now it is confirming that the budget change has occurred even if it did not.

I'm new to programming and suspect that an If-Then statement might not be the best way to search; it seems to work, however I am open to doing away with that concept if there is a better way.

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdSetBudget_Click()
  2.  
  3.     Dim dbJob As DAO.Database
  4.     Dim rsBudget As DAO.Recordset
  5.  
  6.     Set dbJob = CurrentDb
  7.     Set rsBudget = dbJob.OpenRecordset("tblBudget")
  8.  
  9. '   txtSetBudget is a box on frmBudgetControl
  10.     Dim strSetBudget As Currency
  11.     strSetBudget = txtSetBudget
  12.  
  13. '   txtFiscalYear is a box on frmBudgetControl
  14.     Dim intFiscalYear As Integer
  15.     intFiscalYear = txtFiscalYear
  16.  
  17. '   txtOffice is a box on frmBudgetControl
  18.     Dim strOffice As String
  19.     strOffice = txtOffice
  20.  
  21. '   variable to check if fiscal year is four digits
  22.     Dim strCharLength As String
  23.     strCharLength = Len(txtFiscalYear)
  24.  
  25. '   MsgBox to confirm your edit
  26.     Dim intResponse As Integer
  27.  
  28. '   Checks to make sure the fiscal year is four digits.
  29.     If strCharLength = 4 Then
  30.  
  31.         intResponse = msgbox("Clicking yes will change the budget " & _
  32.         "of office number " & strOffice & " to $" & strSetBudget & vbLf & _
  33.         "This action cannot be undone!", vbYesNo)
  34.  
  35.         If intResponse = 6 Then
  36.             With rsBudget
  37.                 Do Until .EOF
  38. '               compares the office ID to the one in the box on the form
  39.                 If !Office_ID = strOffice And !FY = intFiscalYear Then
  40.                     .Edit
  41. '                    Sets the budget to the value in the box on the form
  42.                     !Budget = strSetBudget
  43. '                    This code adds to the budget
  44. '                    !Budget = (!Budget + txtSetBudget)
  45.                     .Update
  46.                 End If
  47.                 .MoveNext
  48.                 Loop
  49.             End With
  50.             msgbox "The budget for office number " & strOffice & _
  51.             vbLf & "has been changed to: $" & strSetBudget
  52.             Else
  53.                 msgbox "Budget not changed", 48
  54.                 Exit Sub
  55.         End If
  56.     Else
  57.         msgbox "You must enter a four digit fiscal year."
  58.         Exit Sub
  59.     End If
  60.  
  61. End Sub
  62.  
Sorry if the code didn't line up, I've never posted before.
Oct 21 '16 #1
8 1240
ADezii
8,834 Expert 8TB
A few changes should do the trick:
Expand|Select|Wrap|Line Numbers
  1. Dim dbJob As DAO.Database
  2. Dim rsBudget As DAO.Recordset
  3. Dim curSetBudget As Currency
  4. Dim intFiscalYear As Integer
  5. Dim strOffice As String
  6. Dim intResponse As Integer
  7.  
  8. Set dbJob = CurrentDb
  9. Set rsBudget = dbJob.OpenRecordset("tblBudget")
  10.  
  11. 'All Fields must contain Data
  12. If IsNull(Me![txtSetBudget]) Or IsNull(Me![txtFiscalYear]) Or IsNull(Me![txtOffice]) Then
  13.   MsgBox "One or more Fields are missing Critical Data", vbExclamation, "Data Missing"
  14.     Exit Sub
  15. End If
  16.  
  17. 'txtSetBudget is a box on frmBudgetControl
  18. curSetBudget = txtSetBudget
  19.  
  20. 'txtFiscalYear is a box on frmBudgetControl
  21. intFiscalYear = CInt(txtFiscalYear)
  22.  
  23. 'txtOffice is a box on frmBudgetControl
  24. strOffice = txtOffice
  25.  
  26. 'Checks to make sure the fiscal year is four digits. Is the Year Numeric and consist
  27. 'of 4 digits?
  28. If IsNumeric(Me![txtFiscalYear]) And Len(CStr(Me![txtFiscalYear])) = 4 Then
  29.   'The combination of [Office_ID] and [fy] must exist in tblBudget
  30.      'DCount("*", "tblBudget", "[Office_ID]='" & "Office A"  & "' And [fy] = " & 2010)"
  31.   If DCount("*", "tblBudget", "[Office_ID]='" & strOffice & "' And [fy] = " & intFiscalYear) > 0 Then
  32.     intResponse = MsgBox("Clicking yes will change the budget " & _
  33.                          "of office number " & strOffice & " to $" & curSetBudget & vbLf & _
  34.                          "This action cannot be undone!", vbYesNo)
  35.       If intResponse = vbYes Then
  36.         With rsBudget
  37.           Do Until .EOF
  38.             'compares the office ID to the one in the box on the form
  39.             If !Office_ID = strOffice And !fy = intFiscalYear Then
  40.               .Edit
  41.                 'Sets the budget to the value in the box on the form
  42.                 !Budget = curSetBudget
  43.               .Update
  44.             End If
  45.               .MoveNext
  46.           Loop
  47.         End With
  48.  
  49.         MsgBox "The budget for office number " & strOffice & _
  50.                 vbLf & "has been changed to: $" & curSetBudget
  51.       Else
  52.         MsgBox "Budget not changed", 48
  53.           Exit Sub
  54.       End If
  55.   End If
  56. Else
  57.   MsgBox "You must enter a four digit fiscal year."
  58.     Exit Sub
  59. End If
  60.  
  61. rsBudget.Close
  62. Set rsBudget = Nothing
Oct 21 '16 #2
This looks great! Thank you so much. Funny how obvious the answer is, but I appreciate that you also made changes to show me what good code looks like. I have no idea what DCount is, even though I can see what it does. I will have to look that one up.

Thanks again!
Jacob
Oct 25 '16 #3
So it looks like the only problem I had with your code was that the validation for the Fiscal Year text box broke when I entered letters. It is the "data type mismatch" error.

I was getting that error before with the DCount section so I changed FY to number from short text.

If I understand correctly CInt is used to convert a string to an integer, is that correct? If so why does it complain about something that is already an integer?
I am also unsure about the DCount error. I think those single quotes are making sure strOffice is looked at as text when it tries to find it in the table?

Sorry for all the questions. I really appreciate the help. I wrote all the code I posted, but I'm still learning why it works and how to use proper syntax.
Oct 25 '16 #4
ADezii
8,834 Expert 8TB
CInt is used to convert a string to an integer, is that correct?
Yes, but you won't need it after the Conversion of Fiscal Year from TEXT to INTEGER.
I think those single quotes are making sure strOffice is looked at as text when it tries to find it in the table?
Correct
  1. Make sure the Data Type of your Fiscal Year Field ([fy]) is set to INTEGER.
  2. Change
    Expand|Select|Wrap|Line Numbers
    1. intFiscalYear = CInt(txtFiscalYear)
  3. to
    Expand|Select|Wrap|Line Numbers
    1. intFiscalYear = txtFiscalYear
  4. Let me know what happens.
Oct 25 '16 #5
That still gave me the "data type mismatch" error.

I got it to work when I changed
Expand|Select|Wrap|Line Numbers
  1. Dim intFiscalYear As Integer
to
Expand|Select|Wrap|Line Numbers
  1. Dim strFiscalYear As String
and left
Expand|Select|Wrap|Line Numbers
  1. strFiscalYear = CStr(txtFiscalYear)
I would like to know why that works though... it seems like the code can't convert an integer because it's already an integer? That doesn't really make sense to me.

I'm curious why strFiscalYear doesn't need the single quotes?
Expand|Select|Wrap|Line Numbers
  1. If DCount("*", "tblBudget", "[Office_ID]='" & strOffice & "' and [FY] = " & strFiscalYear) > 0 Then
Oct 25 '16 #6
ADezii
8,834 Expert 8TB
Are your Fiscal Years truly INTEGERS and Values that cannot be represented as INTEGERS, such as: FY2015, FY2016, etc.
Oct 25 '16 #7
They are just integers. I figured it would be easier that way.
I'm still curious as to why, but the code works now. I really appreciate your help. I found several of your posts on this site that were also very helpful for other things I'm doing. Thanks!
Oct 27 '16 #8
ADezii
8,834 Expert 8TB
@Jake1776:
Glad that you got your Code working and I was happy to assist. Good Luck with your Project.
Oct 27 '16 #9

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

Similar topics

4
by: dr. zoidberg | last post by:
Hello, I'm pulling news titles from MySQL. How Can I display first record in bold (different than other ones). What is the best way to do that. Is there some 'magic' query or PHP function. ...
3
by: luna | last post by:
need to display a label if a record is not found - tried a few things but it appears to be ignoring me!! code is currently like this - you can also see the test i did - must be doing something...
3
by: Meena | last post by:
Hi Every Body, I have created a crystal Report and linked to my vb.net forms and working fine, I added more drill drowns in groups everything fine, but What I need is I am not able to display...
5
by: Jon Bowlas | last post by:
Hi listers, I wrote this script in Zope some time ago and it worked for a while, but now I'm getting the following error: TypeError: coercing to Unicode: need string or buffer, NoneType found ...
1
by: orangetree666 | last post by:
Could someone help me please I have three tables: gallery gallery_images users I am joining data from the three tables the problem is that there are multiple records that appear in the...
1
by: jpr | last post by:
Hello, I have developed a search form with a txtbox and a cmdbutton to open a form based on a query. The cmdbutton opens a form if the record is found. Can anyone help me with VBA to display...
1
by: Coll | last post by:
Hi - I'm trying to figure out how to append a record to a table and then open a form and display that record. My thought was to use the autonum primary key field (recordnum) and display the highest...
6
by: Gilberto | last post by:
Hello I have a table (BOM TOTAL L1 V1) created by a query which only contains one field (TCL1) with ONE record. I've been trying to display this record on a form (either in a textbox or a label)...
0
danieljebaraj
by: danieljebaraj | last post by:
Turn the pages of any magazine for developers and you will see ads for charts. Page after page is filled with images that look almost too good to be true. More than a few appear touched up with...
1
by: srikanthneel | last post by:
Hi i have a method which returns 'image' and i need to display the same image in UI and am using jsf1.2 , icefaces 1.8.2 , Jboss seam. Am adding the snippet i used to display image in UI, but am...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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...
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...
0
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.