Comparing value in Access report field to a query using VBA - ms-access

I currently have a report that lists steps for various tasks and tracks the revision count. Each page is one task, with all of the steps under it. At the end of the report is a Revision history where it will list what was changed to update the revision count. I have two queries for the report, one to generate the report data and the other to generate the revision history.
What I need to do, is to show which tasks have changes on them. I want to do that by comparing the Task_ID on each report page to the Task_ID found in the revision history query.
I've tried a few varitions of dlookups and dcounts without any luck. Whenever I monitor the code, it keeps treating it as false and will end it.
If DLookup("[Task_ID]", "[qry_revision_history_conversions]") = [Reports]![rpt_WI_Book].[Report]![Task] Then
[Reports]![rpt_WI_Book].[Report]![Rev_Change].Visible = False
It's probably something really simple I'm missing, but I can't seem to wrap my head around it.
Here's the code I used to make it work.
Dim LookupTask As Variant
Dim lngRed As Long, lngYellow As Long, lngWhite As Long
lngRed = RGB(255, 0, 0)
lngBlack = RGB(0, 0, 0)
lngYellow = RGB(255, 255, 0)
lngWhite = RGB(255, 255, 255)
LookupTask = DLookup("[Task_ID]", "[qry_task_check]", [Reports]![rpt_WI_Book].[Report]![Text474])
If LookupTask = [Reports]![rpt_WI_Book].[Report]![Text474] Then
[Reports]![rpt_WI_Book].[Report]![Text474].BackColor = lngYellow
Else
[Reports]![rpt_WI_Book].[Report]![Text474].BackColor = lngWhite
End If

Not sure exactly what fields are in your table, but maybe I can assume you're trying to match TaskID to the Report Task field?
From the DLookup docs
if you don't supply a value for criteria, the DLookup function returns a random value
Use your criteria inside DLookup and then check for Null:
Dim LookupTask as Variant
LookupTask = DLookup("[Task_ID]", "[qry_revision_history_conversions]", "[Task_ID] = " & [Reports]![rpt_WI_Book]![Task])
If Not IsNull(LookupTask) Then
[Reports]![rpt_WI_Book]![Rev_Change].Visible = False
End If

Related

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

How to display the count value on label?

I have a monitoring in datagridview that has a display of list of vehicles and their types. I want to count the number of each type of the vehicle.
Example : there are 8 Sedans and 5 Motorcycles in the datagridview and I want it to display in the label.
here is my code:
Dim table As New DataTable()
Dim command As New MySqlCommand("select count(ctype) from tblreport where ctype='SUV'", conn)
command.Parameters.Add("count(ctype)", MySqlDbType.VarChar).Value = Label6.Text
Dim adapter As New MySqlDataAdapter(command)
adapter.Fill(table)
DataGridView2.DataSource = table
After you assign the datasource, just add this :
Label1.Text=datagridview1.rows.count
Text is a property of many controls like label,button and others.This property can be used to display an overlay of text over the control. Datagridview has rows ,right ? So, as u said, u want to display the number of rows the datagridview contains.So all you need to do is use Datagridview1.rows.count,i don't think this one needs any explanation at all.
Anyway, let's advance a bit further.
You also said
there are 8 Sedans and 5 Motorcycles in the datagridview and I want it to display in the label.
By default,no application can understand how many records match Sedans and Motorcycles .But as mentioned above, vb is able to calculate the rows of a datagridview(by default/by simple codes).So try to think for once,if the datagridview only had the rows that contained Sedans or Motorcycles in any cell, vb.net could've easily counted it..right ??
So, if u ask me, how do we get only the rows containing Sedans or Motors , then i'ld as u, how does google show only the results u r looking for instead of showing everything it has ?? The answer is simple,google shows exactly what you Search for. When it comes to databases,we call it filtering/sorting/even seaching.So, all you need to do is filter your database.There are couple of ways,which,if i start explaining,would take a lotta time.However,check these :
• http://csharp.net-informations.com/datagridview/csharp-datagridview-filter.htm
• Filtering DataGridView without changing datasource
Now, let's go deeper.If you go through the links above,you'll be able to show only the rows that contain Sedans or so in any of it's cells.What if you want all the rows but only want the label to display the number(row count) of Sedans or so ?
In this case,you can use a Datatable.
Learn more about Datatable here
You can easily use a datatable to count the rows containg only the QUERY u r looking for.For that, u can code as follows :
Dim cmd as new MySqlCommand("Select * from [tablename](columnname)(#value)",con)
cmd.parametres.addwithvalue("#value","Sedan")
Dim table as new DataTable
Dim adap as new MySqlDataAdapter
adap.fill(table)
If table.rows.count <=0 then
Else
LAbel1.text=table.rows.count
Let me explain from dim table.Here i am declaring a datatable.But before that i am filtering the dataset by using cmd.parametres.addwithvalue.Then,i am using a DataAdapter and filling the DataTable with only th e rows containing SEDAN. Now, focus on If table.Rows.Count <=0 Then ...This indicates if there are no rows(which is less than or equals to 0) containing the query.Then i am using the Else statement which means if the datatable contains rows matching the query.In the else statement,i am adding the row count of the Datatable as a string/text in the Label..
I hope this is enough and this explanation will help u in future as well.
Use the same DataTable you have used to fill the DataGridView. Only change your select statement to Select * from tableName. I used the index of 2 but change to whatever it is in your table. The column index starts at zero.
Dim totalSedans As Integer = 0
Dim totalMotorcycles As Integer = 0
Dim totalConvertable As Integer = 0
For Each row As DataRow In table.Rows
Select Case row(2).ToString
Case "Sedan"
totalSedans += 1
Case "Motorcycle"
totalMotorcycles += 1
Case "Convertable"
totalConvertable += 1
Case Else
Debug.Print("Something doesn't match categories")
End Select
Next
lblConvertable.Text = totalConvertable.ToString
lblMotorcycles.Text = totalMotorcycles.ToString
lblSedan.Text = totalSedans.ToString

How can I check a checkbox in Access as I move from record to record?

I have an Access database which has a checkbox that I need to see if it's "false" or "true" as I go from record to record. If the checkbox is true the label font color and name should change.
This is my code but it's not working properly:
Private Sub Check796_Click()
lngRed = RGB(255, 0, 0)
lngBlack = RGB(0, 0, 0)
If Me.Check796 = vbTrue Then
Me.Label797.Caption = "Hold"
Me.Label797.ForeColor = lngRed
Else
Me.Label797.Caption = "UnHold"
Me.Label797.ForeColor = lngBlack
End If
End Sub
If it's checked it should be red and say "Hold" but, if it's unchecked it should say "UnHold". If I check the "Hold" checkbox it's works but, when I go to the next record which is unchecked it says "Hold" as well. When I close the application and go back into the record with the checkbox clicked the label says "UnHold" when it should say "Hold".
Just execute the code again on Form_Current:
Private Sub Form_Current()
lngRed = RGB(255, 0, 0)
lngBlack = RGB(0, 0, 0)
If Me.Check796 = vbTrue Then
Me.Label797.Caption = "Hold"
Me.Label797.ForeColor = lngRed
Else
Me.Label797.Caption = "UnHold"
Me.Label797.ForeColor = lngBlack
End If
End Sub
To do it without any VBA:
Add your checkbox with the Control Source set to your boolean
(Yes/No) field.
Add a textbox (format to look like a label).
Give it a Control
Source of =Choose(Abs([Check796])+1,'UnHold','Hold')
A boolean
returns -1 for TRUE and 0 for FALSE.
ABS removes the sign, so
it's now 0 and 1.
Add 1 as CHOOSE needs to start at 1.
Select the text and in the FORMAT ribbon in Form Design Tools
select Conditional Formatting.
Select Field Value Is equal to
"Hold" and format as red font.

MS Access - change colors of the bar chart based on series value

I'm trying to write a code in MS Access 2013 that will change the color of the each bar in the chart based on value of the series. Data are stored in query that changes according to the set of conditions.
In the form is chain of combo boxes and listboxes witch set the conditions for query. After requery the output is a graph.
Private Sub listS_AfterUpdate()
Me.graph.Requery
Dim i
With graph
For i = 1 To .SeriesCollection.Count
If .SeriesCollection(i).Value < 0.78 Then
.SeriesCollection(i).Border.Color = RGB(255, 0, 0)
.SeriesCollection(i).Interior.Color = RGB(255, 0, 0)
Else
.SeriesCollection(i).Border.Color = RGB(0, 255, 0)
.SeriesCollection(i).Interior.Color = RGB(0, 255, 0)
End If
Next i
End With
End Sub
The problem lies in the IF statement:
If .SeriesCollection(i).Value < 0.78 Then
Error:
Object doesn't support this property or method
Can anyone help me out with this?
Thank you
I've done this in Excel, but the way I had to do it was to have multiple ranges in the bar chart, each with a different color, and then the data in each range was selected based upon its values. Maybe this would work in Access as well?

Problem updating values in combobox in vb.net

I have this code, but I have a problem.
When I update but do not really made any changes to the value and press the update button, the data becomes null. And it will seem that I deleted the value.
I've taught of a solution, that is to add both combobox1.selectedtext and combobox1.selecteditem to the function. But it doesn't work.
combobox1.selecteditem is working when you try to alter the values when you update. But will save a null value when you don't alter the values using the combobox
combobox1.selectedtext will save the data into the database even without altering.
But will not save the data if you try to alter it.
-And I incorporated both of them, but still only one is performing, and I think it is the one that I added first:
Dim shikai As New Updater
Try
shikai.id = TextBox1.Text
shikai.fname = TextBox2.Text
shikai.mi = TextBox3.Text
shikai.lname = TextBox4.Text
shikai.ad = TextBox5.Text
shikai.contact = TextBox9.Text
shikai.year = ComboBox1.SelectedText
shikai.section = ComboBox2.SelectedText
shikai.gender = ComboBox3.SelectedText
shikai.religion = ComboBox4.SelectedText
shikai.year = ComboBox1.SelectedItem
shikai.section = ComboBox2.SelectedItem
shikai.gender = ComboBox3.SelectedItem
shikai.religion = ComboBox4.SelectedItem
shikai.bday = TextBox6.Text
shikai.updates()
MsgBox("Successfully updated!")
Please help, what would be a simple workaround to solve this problem?
a few things to remember ---
a 'selected____' anything is only non-null when something is, uhm, SELECTED. To ensure that SOMETHING is selected even at start add a line like: ComboBox1.SelectedIndex = 0.
If your recordset has non-string types (like a DATE field might be) then be sure to first check then coerce the string coming back as TEXT to the correct type. I.e....
if isDate(ComboBox1.SelectedText) then ... 'its ok to use this coerced text.
Since a combobox (as well as a listbox) can hold an entire CLASS (i.e. any kind of OBJECT) ... any SelectedItem assignment had better match EXACTLY to the type that was .Items.Add 'ed originally to the control.