I'm trying to write code in Access 2010 that when the [Validate] button is clicked, it will analyze multiple fields (8 in total) for a value (or no value) and then return a statement (or text) in another field ([AppStatus]) based on whether all of the 8 fields are entered or not. In other words, if any of the fields are null, the [AppStatus] field should populate with the default text of "RE PRE-QUAL". Below is where I started but I can't seem to figure out why it is not working.
Private Sub Validate_Click()
If [PrimarySSN].Value = Null Then
If [PropAddress].Value = Null Then
If [PropCity].Value = Null Then
If [PropState].Value = Null Then
If [PropZipCode].Value = Null Then
If [RequestedLoanAmount].Value = Null Then
If [BorrowerIncome.Value] = Null Then
If [EstHomeValue].Value = Null Then
[AppStatus].Value = "RE PRE-QUAL"
ElseIf [PrimarySSN].Value = Not Null Then
ElseIf [PropAddress].Value = Not Null Then
ElseIf [PropCity].Value = Not Null Then
ElseIf [PropState].Value = Not Null Then
ElseIf [PropZipCode].Value = Not Null Then
ElseIf [RequestedLoanAmount].Value = Not Null Then
ElseIf [BorrowerIncome].Value = Not Null Then
ElseIf [EstHomeValue].Value = Not Null Then
[AppStatus].Value = Null
End If
End If
End If
End If
End If
End If
End If
End Sub
One of the reasons it's not working is because Nothing is ever equal to Null, not even another Null. Another issue is you can't use Not Null in VBA code. (Not Null is valid in Access SQL, but that doesn't help here.) In VBA, use IsNull() to check whether a value is Null.
You want "RE PRE-QUAL" as the value of AppStatus whenever one or more of those other 8 fields is Null (ie IsNull(fieldname.Value) = True). Otherwise, AppStatus will be Null. So you could do something like this ...
If IsNull(Me.PrimarySSN.Value) _
Or IsNull(Me.PropAddress.Value) _
' additional Or conditions for each of next 5 fields
Or IsNull(Me.EstHomeValue.Value) Then
Me.AppStatus.Value = "RE PRE-QUAL"
Else
Me.AppStatus.Value = Null
End If
However that would be a bit unwieldy when extended to all 8 fields.
As an alternative, you could start with a list of the control names, load them into an array, and use a For loop to walk the array checking whether each control value is Null. If any of them is Null, set AppStatus to "RE PRE-QUAL" and break out of the loop.
Dim astrFields() As String
Dim strFieldList As String
Dim varAppStatus As Variant
Dim varField As Variant
strFieldList = "PrimarySSN,PropAddress,PropCity,PropState,PropZipCode," & _
"RequestedLoanAmount,BorrowerIncome,EstHomeValue"
astrFields = Split(strFieldList, ",")
varAppStatus = Null
For Each varField In astrFields
'Debug.Print varField
If IsNull(Me.Controls(varField).Value) = True Then
varAppStatus = "RE PRE-QUAL"
Exit For
End If
Next
Me.AppStatus.Value = varAppStatus
Notice this approach could make maintenance easier. If you ever need to add or remove controls from the list of those which should be examined, or change any of their names, simply edit the strFieldList string.
I think that this is what you need:
If [PrimarySSN].Value = Null Or [PropAddress].Value = Null Or [PropCity].Value = Null Or [PropState].Value = Null Or [PropZipCode].Value = Null Or [RequestedLoanAmount].Value = Null Or [BorrowerIncome.Value] = Null Or [EstHomeValue].Value = Null Then
[AppStatus].Value = "RE PRE-QUAL"
Else
[AppStatus].Value = Null
End If
And also consider this:
Related
So I'm relatively new to MS Access but have developed a relatively comprehensive CRM tool for my organization that allows users to record company employees, organization details, and meeting details.
I've run into an odd problem with my "Edit Organization" form. When I use the unbound lookup combobox to change records, it loads up the new record correctly; however, the record number showing at the bottom of the page appears to be random... for example if I select the 6th record using the lookup box, it might show as record number 96 (or something else totally random).
I'm not super concerned by this (although somewhat); however, one of the other odd behaviours is that the lookup works for all records except when changing back to the first organization in the database. In this case, it doesn't load the EmployeeID and related information properly (but this is fine for all other records in the database).
Any suggestions would be great. The code the page is as below. I'm sure there is some extraneous code in there at the moment, but none of it should affect this I don't think...
'Updates the Phone and Email text boxes when the Employee is changed in the dropdown menu
Private Sub cboEmployee_Change()
Me.txtPhone.Value = Me.cboEmployee.Column(2)
Me.txtEmail.Value = Me.cboEmployee.Column(3)
Me.cboRelationship.Value = Null
Me.cboBranch.Requery
Me.cboMinistry.Requery
Me.cboDivision.Requery
End Sub
'Updates the Phone and Email text boxes when the Employee is changed in the dropdown menu
Private Sub cboEmployee_AfterUpdate()
Me.txtPhone.Value = Me.cboEmployee.Column(2)
Me.txtEmail.Value = Me.cboEmployee.Column(3)
Me.cboRelationship.Value = Null
Me.cboBranch.Requery
Me.cboMinistry.Requery
Me.cboDivision.Requery
End Sub
'Requeries the various other boxes on the form after a Ministry is selected and clears the Phone and Email fields. Bit of a hack to use Employee 43 (blank vs. null)
Private Sub cboMinistry_Change()
Me.cboDivision.Value = Null
Me.cboBranch.Value = Null
Me.cboDivision.Requery
Me.cboBranch.Requery
Me.cboEmployee.Requery
Me.txtPhone = Null
Me.txtEmail = Null
End Sub
'Requeries the various other boxes on the form after a Division is selected and clears the Phone and Email fields. Bit of a hack to use Employee 43 (blank vs. null)
Private Sub cboDivision_Change()
Me.cboMinistry.Requery
Me.cboBranch.Requery
Me.cboEmployee.Requery
Me.txtPhone = Null
Me.txtEmail = Null
Me.cboRelationship.Value = Null
End Sub
'Requeries the various other boxes on the form after a Branch is selected and clears the Phone and Email fields. Bit of a hack to use Employee 43 (blank vs. null)
Private Sub cboBranch_Change()
Me.cboDivision.Requery
Me.cboMinistry.Requery
Me.cboEmployee.Requery
Me.txtPhone = Null
Me.txtEmail = Null
Me.cboRelationship.Value = Null
End Sub
'Populates the form with the appropriate details if there is an EmployeeID associated with an organization
Private Sub Form_Current()
Me.cboLookup.Value = Organization
If IsNull(EmployeeID) = True Then
Me.cboMinistry.Value = Null
Me.cboDivision.Value = Null
Me.cboBranch.Value = Null
Me.txtPhone.Value = Null
Me.txtEmail.Value = Null
Else
Me.cboEmployee.Value = EmployeeID
Me.cboBranch.Value = BranchID
Me.cboDivision.Value = DLookup("DivisionID", "tblBranch", "BranchID=" & BranchID)
Me.cboMinistry.Value = DLookup("MinistryID", "tblDivision", "DivisionID=" & Me.cboDivision)
Me.cboEmployee.Requery
Me.cboBranch.Requery
Me.cboDivision.Requery
Me.txtPhone.Value = PhoneNumber
Me.txtEmail.Value = Email
Me.Dirty = False
End If
End Sub
I have a stored procedure for search screen where I have 5 different filters.
Users can leave the filters empty or have one filter and click on search button.
All the data is coming from database, I am trying to create stored procedure which can exclude columns in where clause if the parameter value is empty string.
#Id as nvarchar(256) = 1
#blnIsinProgress bit = 0
#strStatus varchar(20) = ''
#strName varchar(50) = ''
#strConfirmationNumber varchar(50) = 123
#dtmSubmittedFrom Date = '12/31/9999'
#dtmSubmittedTo Date = '12/31/9999'
as
BEGIN
SELECT *
FROM tblTable
WHERE
(#Id IS NULL OR lngID = #Id) AND
(#blnIsinProgress IS NULL OR blnIsinProgress = #blnIsinProgress) AND
(#strStatus = '' OR strStatus = #strStatus) AND
(#strName= '' OR strName= #strName) AND
(#strConfirmationNumber = '' or #strConfirmationNumber = #strConfirmationNumber )
End
But
Execute spManage 1,0,'','',123
will give me all the results
The problem is this line:
(#strConfirmationNumber = '' or #strConfirmationNumber = #strConfirmationNumber )
The second condition is always true. You have an extra #. So, try this:
(#strConfirmationNumber = '' or #strConfirmationNumber = strConfirmationNumber )
i am having a problem in recovering a null date from my database into datepicker in vb.net.
i was able to save a datein mysql with a null value but can't retrieve it to datepicker.
i tried this code but it doesn't work.
If reader.IsDBNull(0) Then
Return
Else
refdate.Text = reader.GetString("refdate")
End If
my code for retrieving is
Try
If e.RowIndex >= 0 Then
Dim row As DataGridViewRow
row = Me.DataGridView1.Rows(e.RowIndex)
forid.Text = row.Cells("id").Value.ToString
Try
connection.Open()
Dim sel As String
sel = "select * from recordtracker where id ='" & forid.Text & "'"
com = New MySqlCommand(sel, connection)
reader = com.ExecuteReader
While reader.Read
Cancel.Show()
Clear.Hide()
rdate.Enabled = False
rfromtbx.Enabled = False
doctype.DropDownStyle = ComboBoxStyle.DropDown
ID.Text = reader.GetInt32("id")
doctype.Text = reader.GetString("Type_of_Document")
itemtbx.Text = reader.GetString("Items")
rfromtbx.Text = reader.GetString("Received_From")
rdate.Text = reader.GetString("Received_Date")
remarks.Text = reader.GetString("Remarks")
margnote.Text = reader.GetString("Marginal_Note")
reftotbx.Text = reader.GetString("Referred_To")
acttaken.Text = reader.GetString("Action_Taken")
'refdate.Text = reader.GetString("refdate")
'If reader.Read() Then
If reader.IsDBNull(0) Then
Return
Else
refdate.Text = reader.GetString("refdate")
End If
Delete.Show()
' End If
End While
Catch ex As Exception
MsgBox(ex.Message)
End Try
any help is appreciated.
For future use, this was the answer needed by the asker.
reader.IsDBNull(0) gets the value of index zero which I presume that refdate is not in the first index.
Using reader.GetString("refdate") gets the String value; therefore, when retrieving null value, it returns error so better use isDbNull(reader("refdate")) to check if the field is null or not. To get the String value, you can simply use reader("refdate").toString
I have an If statement which I was assuming was comparing each value to each other. However it seems no matter what the values are (e.g. all values contain a count of 4) it goes to the else. Am I missing something in the If statement?
If rst![CountOfProvider] = rst![CountOfDelivery Type] = rst![CountOfBU Creator] = rst![CountOfOrigin] = rst![CountOfSub-Destination] = rst![CountOfDestination Zipcode] = rst![CountOfCity] = rst![CountOfState] = rst![CountOfCost Zone] = rst![CountOfRate] = rst![CountOfMarket Name] Then
chk = False
Else
chk = True
End If
VBA doesn't perform that sequence of comparisons as you seem to expect.
Consider this simpler example from the Immediate window ...
Debug.Print 2 = 2
True
Debug.Print 2 = 2 = 2
False
I'm uncertain how VBA handles those multiple equality comparisons, but suspect it may be testing the first and then comparing the result from that with the next ... sort of like this ...
Debug.Print (2 = 2) = 2
False
The first comparison returns True, which is the integer -1 ...
Debug.Print CInt(2 = 2)
-1
So that means the final comparison would be equivalent to this ...
Debug.Print -1 = 2
And naturally that returns False.
The computationally quickest way is to hard code the comparisons. The more
extensible way is to test via a loop.
HansUp makes a good comment - you should be wary of potential null values and add in a handler to deal with them as desired (e.g. using Nz() in Access or IsNull() in any host environment)
'Method 1
If rst![CountOfProvider] = rst![CountOfDelivery Type] And _
rst![CountOfProvider] = rst![CountOfBU Creator] And _
...etc...Then
chk = False
Else
chk = True
End If
'Method 2
chk = True
For Each ele In Array(rst![CountOfDelivery Type], rst![CountOfBU Creator],...your others...)
If ele <> rst![CountOfProvider] Then
chk = False
Exit For
End If
Next ele
Is it possible to edit data that is grabbed from a recordset? In my case, I am trying to add quantities together so that I can get a total. So an example of what I am trying to do would be:
<%
set rs = server.CreateObject("ADODB.recordset")
totalqty = 0
do NOT while rs.EOF
totalqty = totalqty + rs("QTY")
loop
>%
Whenever I tried to do something like this, I would always get an 'Type MisMatch' Error and I'm not sure how to resolve this problem.
As always, any and all help would be appreciated.
Try to "cast" the value in the recordset like so:
CDbl( rs.fields("QTY").value )
This will cast the value to a double. If the value is null you will get en error so you have to check that first...
Or you can write a function to always get the correct type:
public function parse(value, alternative)
dim val
val = trim(value & "")
parse = alternative
if val = "" then exit function
on error resume next
select case varType(parse)
case 2, 3 'integer, long
parse = cLng(val)
case 4, 5 'single, double
parse = cdbl(val)
case 6 'currency
parse = ccur(val)
case 7 'date
parse = cDate(val)
case 11 'bool
parse = cBool(val)
case 8 'string
parse = value & ""
case else
on error goto 0
lib.throwError("type not supported. val:" & value & " alt:" & alternative)
end select
on error goto 0
end function
dim val : val = rs("QTY")
val = parse(val, 0)
' now val is always an integer (either the value from db or 0)
ulluoink's solution will work, but this is simpler...
function ToDbl(vIn, nDefault)
'Convert a variant to an integer using default where necessary
if isnull(vIn) then
ToDbl = nDefault
else
if IsNumeric(CStr(vIn)) Then
ToDbl = CDbl(vIn)
else
ToDbl = nDefault
end if
end if
end function
Then just call:
totalqty = totalqty + ToDbl(rs("QTY"), 0)