MS access SQL query that count/rank between values of other column - ms-access

I have a table in Access 365 with this column:
"tblfreq"
Col1
0
0
1
0
0
0
0
0
1
0
0
1
0
I want to create a query that shows ranks/count(?!) rows between the value "1" of "Col1".
"Col1" only has "0" and "1".
That would create a query with a column "colaux" result like this:
Col1
Caux
0
1
0
2
1
1
0
2
0
3
0
4
0
5
0
6
1
1
0
2
0
3
1
1
0
2

as June 7th said there must be a unique identifier for each row. just add an autonumber column to the table. then you can calculate caux. if you have a fresh autonumber column which will go 1,2, 3, 4, 5, etc then you can actually just subtract id's to get the rows between ids, but I went ahead and counted rows anyway. The expression for calculating caux is long and I needed a function to calculate the first row in tblFreq where col1=1 anyway so I abstracted most everything to the function getCaux.
after adding the autonumber column ID go to the create tab on the ribbon and add a code module with the following
'Utilities
Private firstIDtblFreqcol1is1 As Long
Public Function getfirstIDtblFreqcol1is1() As Long
If firstIDtblFreqcol1is1 = 0 Then
firstIDtblFreqcol1is1 = DMin("ID", "tblFreq", "col1=1")
End If
getfirstIDtblFreqcol1is1 = firstIDtblFreqcol1is1
End Function
Public Function mostrecentrowwithcol1is1(id As Long) As Long
mostrecentrowwithcol1is1 = DMax("ID", "tblFreq", "ID < " & id & " AND col1 = 1")
End Function
'if col1=1 caux =1
'first row where col1=1 is 3 so if id < 3 caux is column count
'else caux = column count(id) - column count(id last row with col1=1)
Public Function getCaux(id As Long) As Long
If DLookup("col1", "tblFreq", "ID = " & id) = 1 Then
getCaux = 1
ElseIf id < getfirstIDtblFreqcol1is1 Then
getCaux = DCount("ID", "tblFreq", "ID <= " & id)
Else: getCaux = DCount("ID", "tblFreq", "ID <= " & id) - DCount("ID", "tblFreq", "ID <= " & mostrecentrowwithcol1is1(id)) + 1
End If
End Function
Then the query is just:

Related

How to add new Access record by checkbox and number of different date?

I have query 1 or table 1 have the below data
Number of employee/start date/end date/code/different date by Days/checkbox
Example
200/01-01-2021/15-01-2021/E/14/Yes
I need when checkbox=Yes to open 14 record automatically in new table 2 with code like below:
employee/date/code
200/01-01-2021/E
200/02-01-2021/E
200/03-01-2021/E
200/04-01-2021/E
200/05-01-2021/E
200/06-01-2021/E
200/07-01-2021/E
200/08-01-2021/E
200/09-01-2021/E
200/10-01-2021/E
200/11-01-2021/E
200/12-01-2021/E
200/13-01-2021/E
200/14-01-2021/E
Extract your parameters:
Data = "200/01-01-2021/15-01-2021/E/14/Yes"
ENo = Split(Data, "/")(0)
FirstDate = DateValue(Split(Data, "/")(1))
ECode = Split(Data, "/")(3)
Periods = Split(Data, "/")(4)
Then run this query passing it the parameters:
PARAMETERS
Periods Short,
FirstDate DateTime;
SELECT DISTINCT
10 * Abs([Deca].[id] Mod 10) + Abs([Uno].[id] Mod 10) + 1 AS Sequence,
ENo As Employee,
DateAdd("d", [Sequence] - 1, [FirstDate]) AS DateStart,
ECode As Code
FROM
MSysObjects AS Uno,
MSysObjects AS Deca
WHERE
(10 * Abs([Deca].[id] Mod 10) + Abs([Uno].[id] Mod 10)) < [Periods];
Output:
You can the use this query as source in an append query.
Or, alternatively, use DAO to directly append the records:
Dim Records As DAO.Recordset
Dim Index As Integer
Set Records = CurrentDb.OpenRecordset("Select * From Table2")
For Index = 0 To Periods - 1
Records.AddNew
Records!Employee.Value = Eno
Records!Date.Value = DateAdd("d", Index, FirstDate)
Records!Code.Value = ECode
Records.Update
Next
Records.Close

MySQL query records from 2 columns

I have a MySQL database with 2 columns. Each column is populated with numbers.
I need to return all records that matches the following criteria:
It will check the first character from column1. If it equals 1 then it will check the last character from column2. If it equals 7 then it will count the total length of column1 + column2. If it equals 12 then it will return this results.
Pseudo code (Vba)
For k = 1 To 100
col1 = Cells(k, 1).Value
For v = 1 To 100
col2 = Cells(v, 2).Value
If Left(col1, 1) = "1" Then
If Right(col2, 1) = "7" Then
If Len(col1) + Len(col2) = 12 Then
MsgBox col1 & "-" & col2
End If
End If
End If
Next
Next
Thanks.
The function names in mysql are almost identical: left(), right(), char_length(). You need to combine them in the where clause with and operator because you want to return records that satisfy all criteria.
select * from yourtable
where Left(col1, 1) = '1' and Right(col2, 1) = '7' and char_length(col1) + char_length(col2) = 12

Using IF to look at multiple fields to return a set value

I have an access database that tracks our internal quality assurance checks for staff. They have 8 items we track them on and each of those items has 4 possible values:
Pass (1)
Non-Pass (2)
FYI (3)
NP&FYI (4).
You can see these 4 set on the left in the column topped by "CPT". [1]. What I would like to have happen is if ANY of those 8 fields are FYI (ID 3) it sets the FYI checkbox to true. If ANY are Non-Pass (ID 2) It sets the passing checkboxes to false. If ANY of the fields are NP&FYI (ID 4) I want the two checkboxes to be set to non-pass and FYI. The code I am trying is:
Private Sub Form_AfterUpdate()
If (Me.Diagnosis And Me.CPT And Me.DOS And Me.UpDownCode And
Me.ChargeCorrections And Me.ChurnEsc And Me.Protocol And Me.BillServProv)
= 1 Then
Me.FYI = 0
Me.QAFail = -1
End If
If (Me.Diagnosis Or Me.CPT Or Me.DOS Or Me.UpDownCode Or
Me.ChargeCorrections Or Me.ChurnEsc Or Me.Protocol Or Me.BillServProv) = 2
Then
Me.QAFail = 0
End If
If (Me.Diagnosis Or Me.CPT Or Me.DOS Or Me.UpDownCode Or
Me.ChargeCorrections Or Me.ChurnEsc Or Me.Protocol Or Me.BillServProv) = 3
Then
Me.FYI = -1
End If
If (Me.Diagnosis Or Me.CPT Or Me.DOS Or Me.UpDownCode Or
Me.ChargeCorrections Or Me.ChurnEsc Or Me.Protocol Or Me.BillServProv) = 4
Then
Me.FYI = -1
Me.QAFail = 0
End If
End Sub
Unfortunately I'm getting inconsistent results. If I set field like CPT to non-pass then the FYI checkbox tics and the QAFail box does not clear. If I set other fields to other values I get all kinds of different results. I'm assuming its because there is confusion between the possible values of the fields being interpreted by the successive If statements. I thought about using CASE, but I couldn't figure out how to set the case value based on 8 different fields. I'm a novice at VBA, can anyone point me in the right direction?
June7's alt code (works)
Dim strCodes As String
With Me
strCodes = Nz(.Diagnosis & .CPT & .DOS & .UpDownCode & _
.ChargeCorrections & .ChurnEsc & .Protocol & .BillServProv, "")
End With
If InStr(strCodes, 2) > 0 Then
Me.QAFail = 0
ElseIf InStr(strCodes, 3) > 0 Then
Me.txtFYI = -1
ElseIf InStr(strCodes, 4) > 0 Then
Me.txtFYI = -1
Me.QAFail = 0
ElseIf strCodes <> "" Then
Me.txtFYI = 0
Me.QAFail = -1
End If
End Sub
The If condition returns either a True or False (-1 or 0) not the 1, 2, 3, 4 you are testing for. Every If block executes so the last one that meets criteria returns result. Use ElseIf then only one End If. Use a With Me block and don't have to repeat the Me qualifier. Why does posted code not show line continuation character? Code assumes every field has a value. If any field is Null or empty string, the test for ID 1 will always return False even if other 7 fields have a 1.
With Me
If (.Diagnosis = 1 And .CPT = 1 And .DOS = 1 And .UpDownCode = 1 And _
.ChargeCorrections = 1 And .ChurnEsc = 1 And .Protocol = 1 And .BillServProv = 1) Then
...
ElseIf (...) Then
...
ElseIf (...) Then
...
ElseIf (...) Then
...
End If
End With
Use that same syntax for the ElseIf expressions using the Or operator.
Make sure testing values in order of priority. If ID 3 should have precedence over 2 and 4 then test for 3 first. I would think 2 has precedence but your narrative contradicts.
Consider alternative code:
Dim strCodes As String
With Me
strCodes = Nz(.Diagnosis & .CPT & .DOS & .UpDownCode & _
.ChargeCorrections & .ChurnEsc & .Protocol & .BillServProv, "")
End With
If InStr(strCodes, 2) > 0 Then
...
ElseIf InStr(strCodes, 3) > 0 Then
...
ElseIf InStr(strCodes, 4) > 0 Then
...
ElseIf strCodes <> "" Then
...
End If

VB.NET Do Until Loop to get the Month Name

I want to get the month name from a Do Until......Loop and display in a CheckedListBox.
I have two tables.
1. Month_Count
2. Fees_Ledger
My month name function...
Function mnthName(ByVal mnth As Integer)
Dim name As String = String.Empty
name = MonthName(mnth, False)
Return name
End Function
To get the Month Details
"SELECT FromMonth,ToMonth,MonthCount FROM Month_Count WHERE SemesterNumber='1'"
And to get the Paid Count
"SELECT COUNT(*) AS TotMonthPaidCount FROM Fees_Ledger WHERE SemYear='1' AND FeeId='1'"
So...
' Got the values from queries
FromMonth = 7 '(July)
ToMonth = 6 '(June)
MonthCount = 12 '(Loop will rotate 12 times)
TotMonthPaidCount = 1 '(FeeId 1 paid one time)
'Declaring an integer variable
Dim StartM As Integer = 1
FromMonth = FromMonth + TotMonthPaidCount
' For the first month
CheckedListBoxMonth.Items.Clear()
CheckedListBoxMonth.Items.Add(mnthName(FromMonth))
' Now the loop to achieve the goal
Do Until StartM = (MonthCount - TotMonthPaidCount)
If FromMonth >= 12 Then
FromMonth = 1
CheckedListBoxMonth.Items.Add(mnthName(FromMonth))
Else
FromMonth += 1
CheckedListBoxMonth.Items.Add(mnthName(FromMonth))
End If
StartM += 1
Loop
This Subroutine results exactly what I want.
But the problem occurs when the StartMonth = FromMonth (6) + TotMonthPaidCount (7) value >12. As 13 or 14 or 15 has no Month Name, it is showing error.
Argument 'Month' is not a valid value.
I want it like below.
What should I do ?

How to insert specific column of datagridview1 to datagridview2? [duplicate]

There are two datagridview (dgvReport and dgvReport2). dgvReport shows data from server after choosing the fields (which is working fine).
The checkboxes are the name of columns in dgvReport. If a user selects "Email" for example the column and its row data of "Email" should be added to dgvReport2.
My problem is when I select more than one checkbox the output of row is shown only at first column of dgvReport2 not under the appropriate column. For example, in the screenshot the column "fname" data is showing under email column of dgvReport2.
How can I bring the row data under appropriate column?
Below is my coding:
'Add dynamic column
Dim newCol As Integer = 0
If chkEmail.Checked = True Then
Dim column As New DataGridViewTextBoxColumn
dgvReport2.Columns.Insert(newCol, column)
With column
.HeaderText = "email"
.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
.ReadOnly = True
End With
For rows As Integer = 0 To dgvReport.Rows.Count - 1
For colcnt As Integer = 0 To dgvReport.Columns.Count - 17
dgvReport2.Rows.Add(dgvReport.Rows(rows).Cells(0).Value)
Next
Next
newCol += 1
End If
If chkFname.Checked = True Then
Dim column As New DataGridViewTextBoxColumn
dgvReport2.Columns.Insert(newCol, column)
With column
.HeaderText = "fname"
.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
.ReadOnly = True
End With
For rows As Integer = 0 To dgvReport.Rows.Count - 1
For colcnt As Integer = 0 To dgvReport.Columns.Count - 17
dgvReport2.Rows.Add(dgvReport.Rows(rows).Cells(1).Value)
Next
Next
newCol += 1
End If
I find out the answer of my own question. Hope fully it will help others who are looking for same issue:
replace
For rows As Integer = 0 To dgvReport.Rows.Count - 1
For colcnt As Integer = 0 To dgvReport.Columns.Count - 17
dgvReport2.Rows.Add(dgvReport.Rows(rows).Cells(0).Value)
Next
Next
with the following:
If dgvReport2.Rows.Count > 0 Then
For rows As Integer = 0 To dgvReport.Rows.Count - 1
dgvReport2.Rows(rows).Cells(newCol).Value =
dgvReport.Rows(rows).Cells(1).Value
Next
Else
For rows As Integer = 0 To dgvReport.Rows.Count - 1
dgvReport2.Rows.Add(dgvReport.Rows(rows).Cells(1).Value)
Next
End If