RecordSet2 mis match with RecordSet DAO.Recordset - ms-access-2013

Holiday Database
I need to know how many people in a particular department are off, so new holiday requests can either be "Approved" or "Rejected"
Table
This is the Table_Add_Holidays
Data for Department
Test Data with 2 departments
Data for the Approved
Just sample Data
I used a Query to perform this it worked then after I changed other code elsewhere it stopped working. So I restarted from scratch using the above code and this doesn't work as I thought it would.
I want to filter the data by "Approved" first then
I want to filter by "Department" so I can get a record count of how many people are off in the department
Any help would be appreciated
TIA
' Dim rstQuery As DAO.Recordset
' Dim rstQuery2 As DAO.Recordset
Dim rstQuery As DAO.Recordset2
Dim rstQuery2 As DAO.Recordset2
On Error GoTo ErrorHandler
' I was using a Query but when I tried "Set rstQuery = rstQuery2.OpenRecordset" I had a mis match
' rstQuery - was RecordSet
' rstQuery2 - whilst this was was RecordSet2
'
' Set rstQuery2 = CurrentDb.OpenRecordset("SELECT * FROM [Query_Table_Add_Holidays_CountOff_General]")
Set rstQuery2 = CurrentDb.OpenRecordset("SELECT * FROM [Table_Add_Holidays]")
rstQuery2.Filter = "StatusOfRequest = 'Approved'"
FindRecordCount = rstQuery2.RecordCount
rstQuery2.Filter = "Department = '" & oDept & "'"
FindRecordCount = rstQuery2.RecordCount
' Set rstQuery = rstQuery2.OpenRecordset
Set rstQuery = rstQuery2
If rstQuery.EOF Then
FindRecordCount = 0
Else
rstQuery.MoveLast
FindRecordCount = rstQuery.RecordCount
End If
rstQuery.Close
Set rstQuery = Nothing

You could DCount:
HolidayCount = DCount("*", "[Table_Add_Holidays]", "StatusOfRequest = 'Approved' And Department = '" & oDept & "'")

Related

display data in list view format after query from databases

I added a filter method which is filter by department by select the combo box options, however, I have no idea about how to push the data into the list after query from database. Below is my code.
Private Sub comboDept_Click()
Dim sQuery As String
Dim oRS As New ADODB.Recordset
Dim oRS_PR As New ADODB.Recordset
Dim sPONO As String
Dim sPOAmt As String
combVal = comboDept.List(comboDept.ListIndex)
If combVal = "EIBU_SALES" Then
sQuery = "Select PO_No, PO_Requestor, PO_Req_Dept, PO_Status, PO_Approval_M, PO_Approval_GM, PO_Approval_D, PO_HRApproval, VC_No, TH_Sup_Inv, PO_HR_Rmk, PO_Req_Date, PO_SupplierName, PO_OverallAmt from PR_INFO where PO_Req_Dept = '" & combVal & "'"
oRS_PR.Open sQuery, PRCnn, adOpenDynamic, adLockOptimistic
ElseIf comboDept.List(comboDept.ListIndex) = "MCBU_SALES" Then
Try something like this (it's just a hint; you have to adjust the code):
' Empty list
myListView.ListItems.Clear
' Add items
While (Not oRS_PR.EOF)
Set item = myListView.ListItems.Add(, , oRS_PR!FIRST_COLUMN)
item.SubItems(...) = oRS_PR!SOME_COLUMN
item.SubItems(...) = oRS_PR!OTHER_COLUMN
oRS_PR.MoveNext
Wend

Search using Combo Box and Filter the results in text boxes

I am trying to do a small program on VB 6.0 to find the records in database and print it in text box based on combo box selection but i failed to find a code which allow me to do this.
Any help please.
Dim adoCon
Dim adoRs
Dim strSQL As String
Dim strDB As String
'Change YourDatabaseName to actual database you have
strDB = "c:\path\YourDatabaseName.accdb"
Set adoCon = CreateObject("ADODB.Connection")
adoCon.Open "Provider = Microsoft.ACE.OLEDB.12.0; " & _
"Data Source = " & strDB & ";" & _
"Persist Security Info = False;"
'Change Table1 to your table name in MS Access
'change the name of combobox and the fieldname in MS Access table
'
'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''
' if combo is numeric
strSQL = "SELECT [fieldNameToReturn] FROM Table1 Where [fieldName] = " + [combo].Value + ";"
' if combo is text
'strSQL = "SELECT [fieldNameToReturn] FROM Table1 Where [fieldName] = '" + [combo].Value + "';"
'''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''
Set adoRs = CreateObject("ADODB.Recordset")
'Set the cursor type we are using so we can navigate through the recordset
adoRs.CursorType = 2
'Set the lock type so that the record is locked by ADO when it is updated
adoRs.LockType = 3
'Open the tblComments table using the SQL query held in the strSQL varaiable
'adoRs.Open strSQL, adoCon
If Not adoRS.Eof() Then
[yourTextBox] = adoRs(0)
End If
adoRs.Close
adoCon.Close
Set adoRs = Nothing
Set adoCon = Nothing
"BOF" below is not a typo. If the recordset is empty (the query returned no records) BOF will be true.
adoRs.Open strSQL, adoCon
If Not adoRS.BOF Then
'If not _BOF_ we have records so load the first record
adoRs.MoveFirst
'If first field is a string then use this
[yourTextBox] = adoRs.Fields(0).Value
'If first field is numeric then use this
[yourTextBox] = CStr(adoRs.Fields(0).Value)
Else
Msgbox "No records returned."
End If
If you were processing multiple records you would still do the MoveFirst and then loop until EOF was true, processing each record. The MoveNext will set EOF = True when there are no more records to process.
adoRs.Open strSQL, adoCon
If Not adoRS.BOF Then
'If not _BOF_ we have records so load the first record
adoRs.MoveFirst
Do While Not adoRS.EOF
'Process records here
'.
'.
'.
adoRS.MoveNext
Loop
Else
Msgbox "No records returned."
End If

VB6. Proceesing ADODB Table Is there a better way of doing this?

I am using .Open to check if record exists.
If it exists, I delete it.
If not, I an adding it.
Then I close the ADODB Recordset.
I am sure there is a better way of doing this - and this is probably a slow way of doing it.
Is there a way of doing this with only one Open and One close?
Here is my code (which is in a Do Loop):
Dim myRecSet As New ADODB.Recordset
Dim strSql As String
strSql = "select * from RentBalances where KeyTcyIdSubAcDate = '" & sKeyTcyIdSubAcDate & "'"
'Display "SQL: " & strSql
myRecSet.Open strSql, SQLSVSExtractConnection, adOpenKeyset, adLockOptimistic
'Display "Total no of records = " & myRecSet.RecordCount
If myRecSet.RecordCount < 1 Then
'Display ("There are no RentBalances record for this ID. ID = " & sKeyTcyIdSubAcDate)
Else
' delete the record
myRecSet.Delete
myRecSet.UpdateBatch
End If
myRecSet.AddNew
myRecSet!KeyTcyIdSubAcDate = rsLocal.Fields("KeyTcyIdSubAcDate")
myRecSet!KeyTcyId = rsLocal.Fields("KeyTcyId")
myRecSet!SubAc = rsLocal.Fields("SubAc")
myRecSet!PeriodEndDate = rsLocal.Fields("PeriodEndDate")
myRecSet!Amount = rsLocal.Fields("Amount")
myRecSet!RentAmount = rsLocal.Fields("RentAmount")
myRecSet!ChargesAmount = rsLocal.Fields("ChargesAmount")
myRecSet!AdjustmentAmount = rsLocal.Fields("AdjustmentAmount")
myRecSet!BenefitAmount = rsLocal.Fields("BenefitAmount")
myRecSet!BenefitBalance = rsLocal.Fields("BenefitBalance")
myRecSet!TenantBalance = rsLocal.Fields("TenantBalance")
myRecSet!PayAmount = rsLocal.Fields("PayAmount")
myRecSet!TimeStamp = rsLocal.Fields("TimeStamp")
myRecSet!UpdateFlag = rsLocal.Fields("UpdateFlag")
myRecSet.Update
myRecCount = myRecCount + 1
myRecSet.Close
The most optimal way of doing this is to bulk insert into a staging table from your code and then call a stored procedure to merge the data from your staging table into your proper table.

can't retrieve dropdownlist.selectedvalue on Page Load?

i have a drop down box , it has value that is retrieve from datasqlsource, however i want to get the count value on page load but it seem that it did not count
'Dim adapter As New SqlDataAdapter
'Dim ds As New DataSet
'Dim connectionString = ConfigurationManager.ConnectionStrings("myProject").ConnectionString
'Dim myConn As New SqlConnection(connectionString)
'Dim cmd = "SELECT * FROM Group Where groupID='" & DropDownList1.SelectedValue & "' AND customerID='" & Session("customerID") & "'"
'Try
' myConn5.Open()
' Dim myCmd5 As New SqlCommand(cmd5, myConn5)
' adapter.SelectCommand = myCmd5
' adapter.Fill(ds, "Group")
' ' txtQuan.Text = adapter.SelectCommand.ExecuteScalar().ToString()
' adapter.Dispose()
' myCmd5.Dispose()
' ' txtQuan.Text = ds.Tables(0).Rows.Count
' If ds.Tables(0).Rows.Count >= 10 Then
' lblNoPpl.Text = "The group is full"
' Else
' Dim numLefts As Integer = 10 - ds.Tables(0).Rows.Count
' lblNoPpl.Text = numLefts.ToString() + "space left"
'Catch ex As Exception
' MsgBox("Can not open connection ! ")
'End Try
if you use cmd.ExcuteScalar() method retrieve a count,the sql should be
SELECT COUNT(*) FROM Group Where groupID='" & DropDownList1.SelectedValue & "' AND customerID='" & Session("customerID") & "'"

List all Password Setting Objects (PSO) using LDAP

how I should proceed to get all active PSOs on a specific domain.
I know that this domain contains the following PSOs:
CN=PSO-Standard
CN=PSO-Sensitive
But I must create a report to display them so I must load them in dynamic way.
I guess there is a kind of filter to get the PSO container and then loop through its recordset.
e.g.
.filter = "(CN=Password Settings Container)"
.attributes = "msDS-PasswordSettingsContainer"
thx in advance.
using classic asp with vbscript
The filter you are looking for is : "(objectClass=msDS-PasswordSettings)"
Here is a sample Vbscript to test :
'==========================================================================
'
' NAME: SearchPSO.vbs
'
' AUTHOR: JPB , Silogix
' DATE : 29/06/2011
'
' COMMENT:
'
'==========================================================================
Option Explicit
Dim machine
Dim oRootDSE ' Root Directory Service Specific Entry
Dim DomainContainer ' The Roor of the Domain
Dim conn ' ADODB connexion
Dim ldapBase ' Base DN of the search
Dim ldapFilter ' Search filter
Dim ldapAttributes ' Attributs to get
Dim ldapScope ' Search scope
Dim ldapStr ' String to execute
Dim rs ' Search result
Dim f '
Dim oADSI ' ADSI access
' ADODB cooking
machine = "WM2008R2ENT"
Set oRootDSE = GetObject("LDAP://"&machine&"/"&"RootDSE")
DomainContainer = oRootDSE.Get("defaultNamingContext")
Set conn = CreateObject("ADODB.Connection")
conn.Provider = "ADSDSOObject"
conn.Properties("User ID") = "jpb"
conn.Properties("Password") = "test.2011"
conn.Properties("Encrypt Password") = True
conn.Open "ADs Provider"
' Building the request to exécute
ldapBase = "<LDAP://" & machine &"/"& DomainContainer & ">"
ldapFilter = "(objectClass=msDS-PasswordSettings)"
ldapAttributes = "cn,msDS-LockoutDuration,msDS-MaximumPasswordAge"
ldapScope = "subtree"
ldapStr = ldapBase&";"&ldapFilter&";"&ldapAttributes&";"&ldapScope
' Search request execution
Set rs = conn.Execute(ldapStr)
' Restitution du résultat
While Not rs.EOF
'For each f in rs.Fields
' WScript.Echo f.Name & ":" & f.Value
'Next
WScript.Echo rs.Fields("cn").Value
rs.MoveNext
Wend

Resources