Access Form - Current Records Count - function

I created a public function, so that I could call it on Access forms, and display the result in a textbox control. This is the public function:
Public Function CurrRecs(xRecName As String, frmName As Form, tblCount As String)
If Forms(frmName).NewRecord Then
frmName.txtCurrRec = "New " & xRecName & " Record"
Else
frmName.txtCurrRec = CStr(frmName.CurrentRecord) & " of " & _
DCount("ID", tblCount) & " " & xRecName & "s"
End If
End Function
This is what I have on the Form_Current()
CurrRecs("RecordType", "frmCurrentForm", "tblGetCountFromHere")
I get a compile error: Expected:=
Anyone know what I'm doing wrong?

The error is because you're declaring frmName as a form, but using it as a string in one place, and a form in another
Also, if you want the table count, use Form.RecordSet.RecordCount, not an elaborate DCount
Rewritten:
Public Function CurrRecs(xRecName As String, frmName As String)
Dim frm As Form
Set frm = Forms(frmName)
If frm NewRecord Then
frm.txtCurrRec = "New " & xRecName & " Record"
Else
frm.txtCurrRec = CStr(frm.CurrentRecord) & " of " & _
frm.RecordSet.RecordCount & " " & xRecName & "s"
End If
End Function

Related

VBA filter working for calculations not for report launch

I'm a beginner so please stick with me. I'm trying to make a user friendly form to calculate total cases for each worker during a date range, and to launch a report based off the same criteria. The calculation is working great but when I click the report it prompts me to enter the parameters for SWNameFilter. Thank you!
Option Compare Database
Option Explicit
Private Sub CalculateResultsSW_Click()
Dim SWReport As DAO.Recordset
Dim vChosenFilters As String
If (Not IsNull(TestSWFromDateFilter)) And (Not IsNull(TestSWToDateFilter)) Then
vChosenFilters = BuildFilterString
CasesCount = DCount("[SPN]", "TestSW", vChosenFilters)
End If
End Sub
Private Sub CloseForm_Click()
DoCmd.Close
End Sub
Private Sub Form_Load()
CalculateResultsSW_Click
End Sub
Private Function BuildFilterString()
BuildFilterString = "(ArrestDate Between #" & TestSWFromDateFilter & "# And #" & TestSWToDateFilter & "#)"
If Not IsNull(SWNameFilter) Then
BuildFilterString = BuildFilterString & " And (SWName = " & "SWNameFilter" & ")"
End If
End Function
Private Sub OpenSWReport_Click()
On Error Resume Next
DoCmd.OpenReport "SWReport", acViewPreview, , BuildFilterString
DoCmd.OutputTo acOutputReport, "SWReport", acFormatPDF, "C:\Users\" & Environ("Username") & "\Desktop\PJDSW Report.pdf", True
DoCmd.Close acReport, "SWReport"
On Error GoTo 0
End Sub
The reason that you see the popup for SWNameFilter is that the query believes that it's a name parameter, and it's asking you to enter a value for that.
The reason for it is simple, this line:
BuildFilterString = BuildFilterString & " And (SWName = " & "SWNameFilter" & ")"
The problem is that you've surrounded your variable name with double-quotes, which basically treats it like text, so your query is becoming:
And (SWName = "SWNameFilter")
This is clearly incorrect, as what you actually wanted was to include the value of the variable:
BuildFilterString = BuildFilterString & " And (SWName = " & SWNameFilter & ")"
Note the removal of the double-quotes, which will then embed the value of the variable in to the SQL instead of the word "SWNameFilter".
If SWNameFilter is a string (which I assume it is) then you'll want some double-quotes to be embedded into the SQL, which you can achieve like this in Access:
BuildFilterString = BuildFilterString & " And (SWName = """ & SWNameFilter & """)"
This will output:
And (SWName = "Value of SWNameFilter")

MS Access hide field name if null value in web page

I want to hide or not show Field Names if value is Null, for fields "Location" and "Bin" when updating my eBay webpage Custom Label Fields.
Tried
If IsNull(Me!Location) Then
Me!Location.Visible = False
Else
Me!Location = True
but keep getting errors (Method or data not found or runtime 438) with the following code.
Call IE.Document.getElementById("editpane_skuNumber").setAttribute("value", ([CustomLabelCombine]) & " " & ("Location" & " " & [Location]) & " " & ("Bin" & " " & [Bin]))
If you have a better solution please let me know.
Is IE.Document.GetelementsbyID a subroutine or function? if not, remove the word "call" and replace with
me.Location = IE.Document.getElementById("editpane_skuNumber").setAttribute("value", ([CustomLabelCombine]) & " " & ("Location" & " " & [Location]) & " " & ("Bin" & " " & [Bin]))
If IsNull(Me!Location) Then
Me!Location.Visible = False
Else
Me!Location = True
End IF
Comment1:
When using "Call" you are asking the current procedure to "Call" a public subroutine. Example below
Private Sub Button1_Click()
Call TestRoutine
End Sub
Public Sub TestRoutine()
Msgbox "You clicked Button 1"
End Sub
If you aren't calling another subroutine to execute more coding, then remove the word "Call".
I also noticed your if is missing the .visible after the else for line
Me!Location = true
Should be:
Me!Location.Visible = true

Get value of node in MS Access using JSOn

I am using VB-JSON parser to get data from an API and saving the data in MS access table.
I am not able to figure out how to access the -KLj9kXnKd-9txfyIqM8 and -KLjJoT7gXCMq_jHx2_z.
I have Table structure as below, and want to save the data as shown below.
|ServerID |Name |Mobile
|-KLj9kXnKd-9txfyIqM8 |Adarsh |9987
|-KLjJoT7gXCMq_jHx2_z |Manas |022
JSON
{
"-KLj9kXnKd-9txfyIqM8": {
"personmobile": "9987",
"personname": "Adarsh"
},
"-KLjJoT7gXCMq_jHx2_z": {
"personmobile": "022",
"personname": "Manas"
}
}
VBA
Public Sub GetPerson()
'I have code here which gets the json as above from api.
Dim egTran As String
If reader.Status = 200 Then
Set db = CurrentDb
Set rs = db.OpenRecordset("tblPerson", dbOpenDynaset, dbSeeChanges)
egTran = "[" & reader.responseText & "]"
Set coll = Json.parse(egTran)
For Each contact In coll
rs.AddNew
rs!Name = contact.Item("personname")
rs!Mobile = contact.Item("personmobile")
rs!ServerID = contact.Item("??????")
what do I write in ??????
rs.Update
Next
End If
End Sub
I am also open to using any other parser. The API is based on Firebase Database
I'm not familiar with VB-JSON, but obviously "??????" is not an item of contact.
Thus, if I run this test function:
Public Sub TestJsonText()
Dim DataCollection As Collection
Dim ResponseText As String
ResponseText = _
"{" & _
" ""-KLj9kXnKd-9txfyIqM8"": {" & _
" ""personmobile"": ""9987""," & _
" ""personname"": ""Adarsh""" & _
" }," & _
" ""-KLjJoT7gXCMq_jHx2_z"": {" & _
" ""personmobile"": ""022""," & _
" ""personname"": ""Manas""" & _
" }" & _
"}"
If ResponseText <> "" Then
Set DataCollection = CollectJson(ResponseText)
MsgBox "Retrieved" & Str(DataCollection.Count) & " root member(s)", vbInformation + vbOKOnly, "Web Service Success"
End If
Call ListFieldNames(DataCollection)
Set DataCollection = Nothing
End Sub
using the Json modules from VBA.CVRAPI it will print:
root
-KLj9kXnKd-9txfy
personmobile 9987
personname Adarsh
-KLjJoT7gXCMq_jH
personmobile 022
personname Manas
From the function ListFieldNames you can pick MemberName for the field name and DataCollection(Index)(CollectionItem.Data) for the field value to add records.

Open a form based on data from a recordset

I have an app that searches through customer information on a SQL Server database.
There are three forms:
The first allows the user to lookup info based on several different criteria, like Account no, Last name Phone Number, etc.
The second form correctly displays the information from the first form.
I want to open a third form based on a value in the result set on the second form, a field called Master_ID.
I have created a Global variable that stores the contents of the Master_ID field on the second form when the Master_ID field is clicked.
Then I want to execute the on click event on a button that contains the following code:
Private Sub tranHisBtn_Click()
Dim transferSQL As String
transferSQL = "SELECT dbo_Master_Accounts.Master_ID, dbo_Master_Accounts.FirstName, dbo_Master_Accounts.LastName, dbo_Transaction_Table.Date_of_Transaction, Format([dbo_Transaction_Table]![Time_of_Transaction],""hh:nn:ss ampm"") AS TranTime, dbo_Transaction_Table.Sku, dbo_Transaction_Table.Description, Right([dbo_Transaction_Table]![Description],6) AS tranAccnt, [dbo_Transaction_Table]![ArAmt]*-1 AS Amnt, dbo_Master_Accounts.Master_ID " & vbCrLf
transferSQL = transferSQL + "FROM dbo_Master_Accounts INNER JOIN dbo_Transaction_Table ON dbo_Master_Accounts.Master_ID = dbo_Transaction_Table.Account_Number " & vbCrLf
transferSQL = transferSQL + "WHERE (((dbo_Transaction_Table.Description) Like ""%Transfer To%"") AND ((dbo_Master_Accounts.Master_ID)=" & Chr$(34) & GBL_Master_Id & Chr$(34) & ")) " & vbCrLf
transferSQL = transferSQL + "ORDER BY dbo_Transaction_Table.Date_of_Transaction, Format([dbo_Transaction_Table]![Time_of_Transaction],""hh:nn:ss ampm"");"
Dim trancon As ADODB.Connection
Set trancon = CurrentProject.Connection
Dim tranRs As New ADODB.Recordset
tranRs.ActiveConnection = trancon
tranRs.CursorType = adOpenStatic
tranRs.Open transferSQL
'DoCmd.OpenForm "TransferbyNumFM" ', , , "Master_Id = 'transRs.fields(0)'"
MsgBox "good"
End Sub
I can't figure out how to set the record source for the third form. Everything runs perfectly until I try to open the third form.
As I said: Open the form with DoCmd and set the recordsource of the Form object in the OnOpen Event of the 3rd form to the SQL string you have. No need to create a recordset, either. And never try to pass a recordsource as a filter in a DoCmd.OpenForm
Oh, and my error: since you have a global variable (i.e. a public variable declared in a module) you don't even need to pass it as an OpenArgs, you can simply do the following on Button Click
DoCmd.OpenForm "TransferbyNumFm"
and then, in the class module of Form3 do
Option Compare Database
Option Explicit
Private Sub Form_Open(Cancel As Integer)
Dim transferSQL As String
transferSQL = _
"SELECT dbo_Master_Accounts.Master_ID, " & _
"dbo_Master_Accounts.FirstName, " & _
"dbo_Master_Accounts.LastName, " & _
"dbo_Transaction_Table.Date_of_Transaction, " & _
"Format([dbo_Transaction_Table]![Time_of_Transaction],'hh:nn:ss ampm') AS TranTime, " & _
"dbo_Transaction_Table.Sku, dbo_Transaction_Table.Description, " & _
"Right([dbo_Transaction_Table]![Description],6) AS tranAccnt, " & _
"[dbo_Transaction_Table]![ArAmt]*-1 AS Amnt " & _
"FROM dbo_Master_Accounts " & _
"INNER JOIN dbo_Transaction_Table ON dbo_Master_Accounts.Master_ID = dbo_Transaction_Table.Account_Number " & _
"WHERE(((dbo_Transaction_Table.Sku) Like '%Transfer%')) " & _
"ORDER BY dbo_Transaction_Table.Date_of_Transaction"
Me.RecordSource = transferSQL
End Sub
The Recordsource of Form3 can be unassigned before this point, though you will need it to add fields (but you can remove it later on)

Issue with Data passing from one form to another form in MS Access VBA

Can anyone solve my issue
Form: SB_1 (this is a Customer Invoice creation form)
Form: Add_NewRelations (this is a Customer details adding form)
I have used a VBA code in Form: SB_1 on a cmd (Button) for Data passing from one form to another form of Add_NewRelations for New Customer details are needed to update.
So that VBA working is good as per Code Tag. But some of exist customer details are no need to update into Form: Add_NewRelations. So in this case i have added IF Statement with DCount in the VBA code.
When i added If statement with DCount then whole VBA is didn't working... i think DCount is wrong... how can i correction it??
Can anyone please replay how to solve this problem??
Private Sub Button_Click()
On Error GoTo ErrorHandler
'Me.Refresh
Dim strCriteria As String
strCriteria = "CIDCustomer = '" & Trim(Trim(Me!RID) & " " & Trim(Me!RName)) & "'"
'If DCount("*", "CIDCustomer", "RelationsQry") > 0 Then
'Cancel = True
'Else
If DCount("*", "RelationsQry", strCriteria) > 0 Then
Cancel = True
Else
'If Not IsNull([Customer]) Then
'Me.Visible = False
DoCmd.OpenForm "Add_NewRelations", acNormal, , , , acWindowNormal
Forms![Add_NewRelations].Form.RID = Me.CID2
Forms![Add_NewRelations].Form.RName = Me.Customer
Forms![Add_NewRelations].Form.RType.Value = "Customer"
Forms![Add_NewRelations].Form.Address = Me.Address
Forms![Add_NewRelations].Form.TINNumber = Me.TINNumber
Forms![Add_NewRelations].Form.TownVLG = Me.TownVLG
Forms![Add_NewRelations].Form.RName.SetFocus
'Forms![Add_NewRelations].Visible = False
'DoCmd.Close
End If
ErrorHandler:
End Sub
To pass the quotes into the SQL you need to escape them, and you need additional single quotes as well for the elements of the criteria.
Instead of:
strCriteria = "CIDCustomer = '" & Trim(Trim(Me!RID) & " " & Trim(Me!RName)) & "'"
try
strCriteria = "" "CIDCustomer = '" & Trim(Trim(Me!RID) & "' & " " & '" Trim(Me!RName)) & "'" ""