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. -
Private Sub cmdSetBudget_Click()
-
-
Dim dbJob As DAO.Database
-
Dim rsBudget As DAO.Recordset
-
-
Set dbJob = CurrentDb
-
Set rsBudget = dbJob.OpenRecordset("tblBudget")
-
-
' txtSetBudget is a box on frmBudgetControl
-
Dim strSetBudget As Currency
-
strSetBudget = txtSetBudget
-
-
' txtFiscalYear is a box on frmBudgetControl
-
Dim intFiscalYear As Integer
-
intFiscalYear = txtFiscalYear
-
-
' txtOffice is a box on frmBudgetControl
-
Dim strOffice As String
-
strOffice = txtOffice
-
-
' variable to check if fiscal year is four digits
-
Dim strCharLength As String
-
strCharLength = Len(txtFiscalYear)
-
-
' MsgBox to confirm your edit
-
Dim intResponse As Integer
-
-
' Checks to make sure the fiscal year is four digits.
-
If strCharLength = 4 Then
-
-
intResponse = msgbox("Clicking yes will change the budget " & _
-
"of office number " & strOffice & " to $" & strSetBudget & vbLf & _
-
"This action cannot be undone!", vbYesNo)
-
-
If intResponse = 6 Then
-
With rsBudget
-
Do Until .EOF
-
' compares the office ID to the one in the box on the form
-
If !Office_ID = strOffice And !FY = intFiscalYear Then
-
.Edit
-
' Sets the budget to the value in the box on the form
-
!Budget = strSetBudget
-
' This code adds to the budget
-
' !Budget = (!Budget + txtSetBudget)
-
.Update
-
End If
-
.MoveNext
-
Loop
-
End With
-
msgbox "The budget for office number " & strOffice & _
-
vbLf & "has been changed to: $" & strSetBudget
-
Else
-
msgbox "Budget not changed", 48
-
Exit Sub
-
End If
-
Else
-
msgbox "You must enter a four digit fiscal year."
-
Exit Sub
-
End If
-
-
End Sub
-
Sorry if the code didn't line up, I've never posted before.
8 1240
A few changes should do the trick: - Dim dbJob As DAO.Database
-
Dim rsBudget As DAO.Recordset
-
Dim curSetBudget As Currency
-
Dim intFiscalYear As Integer
-
Dim strOffice As String
-
Dim intResponse As Integer
-
-
Set dbJob = CurrentDb
-
Set rsBudget = dbJob.OpenRecordset("tblBudget")
-
-
'All Fields must contain Data
-
If IsNull(Me![txtSetBudget]) Or IsNull(Me![txtFiscalYear]) Or IsNull(Me![txtOffice]) Then
-
MsgBox "One or more Fields are missing Critical Data", vbExclamation, "Data Missing"
-
Exit Sub
-
End If
-
-
'txtSetBudget is a box on frmBudgetControl
-
curSetBudget = txtSetBudget
-
-
'txtFiscalYear is a box on frmBudgetControl
-
intFiscalYear = CInt(txtFiscalYear)
-
-
'txtOffice is a box on frmBudgetControl
-
strOffice = txtOffice
-
-
'Checks to make sure the fiscal year is four digits. Is the Year Numeric and consist
-
'of 4 digits?
-
If IsNumeric(Me![txtFiscalYear]) And Len(CStr(Me![txtFiscalYear])) = 4 Then
-
'The combination of [Office_ID] and [fy] must exist in tblBudget
-
'DCount("*", "tblBudget", "[Office_ID]='" & "Office A" & "' And [fy] = " & 2010)"
-
If DCount("*", "tblBudget", "[Office_ID]='" & strOffice & "' And [fy] = " & intFiscalYear) > 0 Then
-
intResponse = MsgBox("Clicking yes will change the budget " & _
-
"of office number " & strOffice & " to $" & curSetBudget & vbLf & _
-
"This action cannot be undone!", vbYesNo)
-
If intResponse = vbYes Then
-
With rsBudget
-
Do Until .EOF
-
'compares the office ID to the one in the box on the form
-
If !Office_ID = strOffice And !fy = intFiscalYear Then
-
.Edit
-
'Sets the budget to the value in the box on the form
-
!Budget = curSetBudget
-
.Update
-
End If
-
.MoveNext
-
Loop
-
End With
-
-
MsgBox "The budget for office number " & strOffice & _
-
vbLf & "has been changed to: $" & curSetBudget
-
Else
-
MsgBox "Budget not changed", 48
-
Exit Sub
-
End If
-
End If
-
Else
-
MsgBox "You must enter a four digit fiscal year."
-
Exit Sub
-
End If
-
-
rsBudget.Close
-
Set rsBudget = Nothing
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
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.
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 - Make sure the Data Type of your Fiscal Year Field ([fy]) is set to INTEGER.
- Change
- intFiscalYear = CInt(txtFiscalYear)
- to
- intFiscalYear = txtFiscalYear
- Let me know what happens.
That still gave me the "data type mismatch" error.
I got it to work when I changed - Dim intFiscalYear As Integer
to - Dim strFiscalYear As String
and left - 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? - If DCount("*", "tblBudget", "[Office_ID]='" & strOffice & "' and [FY] = " & strFiscalYear) > 0 Then
Are your Fiscal Years truly INTEGERS and Values that cannot be represented as INTEGERS, such as: FY2015, FY2016, etc.
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!
@Jake1776:
Glad that you got your Code working and I was happy to assist. Good Luck with your Project.
Sign in to post your reply or Sign up for a free account.
Similar topics
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.
...
|
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...
|
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...
|
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
...
|
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...
|
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...
|
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...
|
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)...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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: 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,...
|
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...
| |