Classic ASP Variable comparison issue - ms-access

I've inherited a very large Classic ASP site that has recently had an error discovered. It reads and writes from an Access database multiple times, and all of that is working as intended. However, I believe there is an error in this part of the code:
Dim SeptAvg, mvUPreq, mvSeptAvg
SeptAvg = CDbl(rs.fields(5))
mvUPreq = -0.15
if SeptAvg <= mvUPreq then
mvSeptAvg = true
else
mvSeptAvg = false
end if
Now in this case, the SeptAvg in the access database is -8.5%. This expression should be evaluating to True - but it's not. Immediately after this code, there is an update to the Access database that sets the field (which is a checkbox in the Access database) to true or false. The checkbox is always left unchecked. I have checked and rechecked the database update code and I know there are no errors there. Is there something happening with the CDbl on the -8.5% value from the database?
Please advise, because I have very limited Classic ASP experience.

-8.5% = -0.085 and -0.085 is indeed larger than -0.15. This is why it is evaluating to true
-1 > -10
Abs(1) < Abs(10)
1 < 10

If you want it evaluated as TRUE, you need to tweak your code a little bit :
Dim SeptAvg, mvUPreq, mvSeptAvg
SeptAvg = CDbl(rs.fields(5))
mvUPreq = -0.15
if abs(SeptAvg) <= abs(mvUPreq) then
mvSeptAvg = true
else
mvSeptAvg = false
end if

-8.5% expressed as a decimal is -0.085
So the If condition is equivalent to ...
if -0.085 <= -0.15 then
But -0.085 is greater than -0.15, so the condition is not True, so mvSeptAvg does not get set to True.

Related

microsoft access Field must be blank or have a value that is a specific length

I have a field on a form and I need the user to leave it blank or enter a value that is a specific length (20 characters). Does anyone have code that may solve this need?
I have tried:
(Len([SIM / ENGRV]) = 20) or (isnull([SIM / ENGRV])) or ([SIM / ENGRV]="")
I assume the control (field) name in the form is Text1.
So you can use this code in the before update event .
Of course, the code can be much shorter,
But I think that's the clearest way to understand the logic.
Private Sub Text1_BeforeUpdate(Cancel As Integer)
Dim varTmp As Variant
' first check if [ENGRV] > 0 to avoid devision by zero error
If Not IsNumeric([ENGRV]) And [ENGRV] = 0 Then
Text1.Undo
Cancel = True
Else
' now avoid [SIM] is null error
If Nz([SIM], "") = "" Then
Text1.Undo
Cancel = True
Else
varTmp = [SIM] / [ENGRV]
' now we know that varTmp is somthing and not empty then check the length
If Len(Trim(str(varTmp))) <= 20 Then
Text1.Undo
Cancel = True
End If
End If
End If
End Sub
You can do that at the table level. Set the Validation Rule of the field to:
Len([SIM / ENGRV])=20 Or [SIM / ENGRV] Is Null

MS Access VBA, efficient way to enable a button only after all required textboxes contains valid data

My code is working, but I just want to know if there is a more efficient way to achieve the same effect.
I have this layout in a form:
In my effort to foolproof the record creation process, I would like to have the "Save and Clear fields" button enabled only after all but the 'Comment' textbox/combobox contains some valid data.
The text/combo boxes are called txtBatteryID, cmbModelNumber, cmbChemistryType, txtSpecVoltage, txtSpecCapacity.
My code is as follow
Private Sub EnableSaveBtnCheck()
'this checks if the required fields contains valid data, if so, enables the save button.
If Me.btnSaveAndCLear.Enabled = False Then
If IsNull(txtBatteryID) = False And IsNull(cmbModelNumber) = False And IsNull(cmbChemistryType) = False And IsNull(txtSpecVoltage) = False And IsNull(txtSpecCapacity) = False Then
Me.btnSaveAndCLear.Enabled = True
End If
End If
End Sub
As you can see, I did the most straightforward way of using AND to combine all must-have conditions in an IF statement. This sub is called in After_Update() event of each text/combo box. Like this:
Private Sub cmbChemistryType_AfterUpdate()
Call EnableSaveBtnCheck
End Sub
My question, in the end, is: Is there a more efficient way to setup the condition "all text/combo box need to have something valid in them"? And is there a more elaborate way to check if the condition is met (something like a event on the form itself)?
Add the values of those 5 fields. If any of them is Null, the sum will be Null. So you only need call IsNull() once.
If IsNull(txtBatteryID + cmbModelNumber + cmbChemistryType + txtSpecVoltage + txtSpecCapacity) = False Then

Access remove line break in string, program tells it's NULL

I have code that does something then you press enter in textfield, problem is when you use Ctrl+Enter, i can capture that event but access tells me in next line that that field is apparently NULL
Private Sub Text5_KeyPress(KeyAscii As Integer)
If KeyAscii = vbKeyReturn Or KeyAscii = 10 Then
If Len(Me.Text5) = 0 Then Exit Sub
If Val(Right(Me.Text5, 1)) > 2 Then Me.Text5 = Left(Me.Text5, Len(Me.Text5) - 1) & "0"
So 'Len' works fine, but the 'Right' function gives out 'Invalid use of null', when i hit debug and check the value it is NULL
I can't figure it out
I guess i need to remove new line characters but how to do that when the text box is null and every function for strings spits out that error
The problem with your check is that Len(Null) is not 0, it's Null.
There are a couple ways to get around this. First, as mentioned in the comments, you can simply add a check for IsNull:
If IsNull(Me.Text5) Or Len(Me.Text5) = 0 Then
The other way you can do this is force it to coalesce by concatenating vbNullString:
If Len(Me.Text5 & vbNullString) = 0 Then
Also you could use Nz and set a return value of your wish in case if the expression is null, in this example also vbNullString and check the result of this function:
If Nz(Me.Text5, vbNullString) = vbNullString Then
or
If Len(Nz(Me.Text5, vbNullString) = 0) Then
or
If Nz(Me.Text5, 0) = 0 Then
or
If Not Nz(Me.Text5, False) Then
For sure you can store the result in a variable first and then check and work with this later on.
Whatever fulfills your needs.
Well, i test it as much as i can and it's just that when you use Ctrl+Enter on a field and capture the key press, field will be null for some reason, i don't see possible way around this

invalid property value in vb6

i have a code right here this is for saving records to databse:
If mstrMaintMode = "ADD" Then
lngIDField = GetNextCustID()
strSPName = "InsertCustomer"
Set objNewListItem = mylistview.ListItems.Add(, , txtname.Text)
PopulateListItem objNewListItem
With objNewListItem
**.SubItems(mlngCUST_ID_IDX) = CStr(lngIDField)**
.EnsureVisible
End With
Set mylistview.SelectedItem = objNewListItem
Set objNewListItem = Nothing
Else
lngIDField = CLng(mylistview.SelectedItem.SubItems(mlngCUST_ID_IDX))
strSPName = "UpdateCustomer"
mylistview.SelectedItem.Text = txtname.Text
PopulateListItem mylistview.SelectedItem
End If
the error is: invalid property value in the line with asterisks. ive tried using this code to another database and it works, but for the other it's not.ive checked the stored procedure, it's right, the table fields, also right but im still getting this error.ive spent 3 hrs to find the answer but i culdn't figure it out.
The line you have highlighted will fail with "Invalid property value" when you specify a sub item index that is out of bounds given the number of columns in the listview.
As the index is 1 based but starting from the second column, with your index of 7, you need at least 8 columns added.

SSRS report calling stored procs

We're using SSRS 2008 R2
We have several reports that call multiple stored procedures. It seems that as soon as the report is called its runs the stored procs with their default values then reruns the stored procs with the passed parameters. Does that make sense?
We're considering using snapshots to store a snapshot of report with all the default parameters but is there a better way?
rptViewer.ProcessingMode = ProcessingMode.Remote
rptViewer.ShowCredentialPrompts = False
rptViewer.ShowBackButton = False
rptViewer.ShowDocumentMapButton = False
rptViewer.ShowExportControls = False
rptViewer.ShowFindControls = False
rptViewer.EnableViewState = True
rptViewer.ShowPageNavigationControls = False
rptViewer.ShowParameterPrompts = True
rptViewer.ShowRefreshButton = False
rptViewer.ShowPrintButton = True
rptViewer.ShowPromptAreaButton = False
rptViewer.ShowToolBar = True
rptViewer.ShowZoomControl = False
rptViewer.SizeToReportContent = True
rptViewer.AsyncRendering = False
rptViewer.Height = Unit.Percentage(100)
rptViewer.Width = Unit.Percentage(100)
Dim RepParameters As New ReportParams
With RepParameters
.ApplicationID = MyBase.CurrentApplicationID.ToString
.EntityID = EntityIDList
If ShowTitle Then .isExported = "True" Else .isExported = "False"
.LanguageID = CShort(MyBase.CurrentLanguage.ID).ToString
.UserSecurityID = CInt(MyBase.CurrentLoggedUser.SecurityID).ToString
End With
**rptViewer.ServerReport.SetParameters(rep.SsrsReportParameters(RepParameters))**
Sounds like you are passing your parameters too late for the reportviewer control. Make sure in the page's execution cycle you are setting them preferably as soon as possible. I worked around this problem by initialising the ReportViewer control in the page load code behind code rather than declaring one in the aspx part. This solved it for me.
More of a workaround than a solution.
We ended up setting the default value to one of the parameters to -1 and in the stored procedure we only run the code if the parameter is not -1.