VBA get value having string containing listbox control name - ms-access

Silly question but I got lost.
In a VBA function I've some text which contains a string representing a listbox control name (ctrlName is the variable containing the control name).
VarText should give back the value of the selected (row/column) from listbox (error occurred here).
Function getString_email(..)
Dim ctrlName As String
Dim getString_email as String
Dim sBody as String
Dim ctl As Control
Dim frm As Form
...
...
sBody = RS("Body")
ctrlName = Mid(sBody, StartVar, EndVar - StartVar)
Set frm = Forms("frmCatalogue")
VarText = frm.Controls(ctrlName)
Set frm = Nothing
...
...
getString_email = Replace(sBody, ctrlName, VarText)
...
error: Run-time error 2465. can't find the field referred to in your expression.
Any help will be great!

Related

Excel VBA Run-time error '13': Type mismatch using JsonConverter

Getting Error on "Excel VBA Run-time error '13': Type mismatch" using JsonConverter
my JSON
{"gstin":"33A","fp":"062020","b2b":[{"ctin":"33B","cfs":"Y","cfs3b":"Y","inv":[{"itms":[{"num":1801,"itm_det":{"csamt":0,"samt":83.97,"rt":18,"txval":933,"camt":83.97}}],"val":1050.94,"inv_typ":"R","pos":"33","idt":"10-06-2020","rchrg":"N","inum":"C3/071","chksum":"60a9044051e8b6ba1122f614143a4d1236b1399872b0ea408df6a82ba832253d"}],"fldtr1":"25-Jul-20","flprdr1":"Jun-20"}]}
my Code
Private Sub CommandButton1_Click()
Dim fd As Office.FileDialog
Set fd = Application.FileDialog(msoFileDialogFilePicker)
With fd
.Title = "Select Json files"
.AllowMultiSelect = False
If .Show() Then
Filename = .SelectedItems(1)
Dim content As String
Dim iFile As Integer: iFile = FreeFile
Open Filename For Input As #iFile
content = Input(LOF(iFile), iFile)
Dim products As Object, Item
Set products = JsonConverter.ParseJson(content)
i = 1
For Each Item In products
Debug.Print Item("gstin")
'Cells(i, 1) = Item("ctin")
'i = i + 1
Next
Close #iFile
End If
End With
End Sub
also need to implement root and keys (like: gstin, ctin, csamt, inum)
Thanks
Please limit to a single question. Your current error is because products is a dictionary and the keys are strings. You cannot do Item("gstin") as Item is a string. You would want initially products(Item) but won't be able to just use Debug.Print as not all the associated values in the dictionary are simple datatypes e.g. products("b2b") will return a collection and lead to a RTE 450 error due to incorrect syntax.
You will need to develop your code to test for which datatype is returned from the dictionary and any nested levels. [] indicates a collection you can For Each over, whereas {} indicates a dictionary. There are lots of examples on SO to help you with this and code examples that will write the entire structure out for you.

Passing a variable to .Value function

I am trying to set field values in a table using .Value, but using a variable for the field name (rs3!variable.Value=anothervariable), but it keeps giving me a runtime error (3265 - Item not found in collection). I have tried placing {} and () around the variable, and it did not help. Anyone know how I use a variable for the field name?
Pertinent Code:
Private Sub GetCMLData_Click()
Dim IPinfo1 As String
Dim rs3 As Recordset
Set rs3 = db.OpenRecordset("CMLData", dbOpenDynaset)
Dim AIP2 as string
AIP2="SomeValue"
rs3.AddNew
rs3!IPinfo1.Value = AIP2(i)
rs3.Update
End Sub
instead of using:
rs3!variableName.Value = ...
you would use:
rs3.Fields(variableName) = ...
Or just - as the Value property of a field is the value it holds:
rs(IPinfo1).Value = AIP2

Subscript out of range - Excel.Application.WorksheetFunction.CountA

I'm trying to count the number of non-empty cells in an Excel spreadsheet from MS-Access VBA.
The idea is that I click a button on a form in MS-Access and the number of non-empty cells are stored as string.
The Excel workbook file is called "MattExcelFile.xls", the sheet within that workbook is called "Sheet1" and the range I want to use Excel's COUNTA function on is "C1:C500".
Here's the code I've pieced together so far:
Option Compare Database
Sub ImportDataFromRange()
Dim xlFilePath As String
Dim rowVariable As String
xlFilePath = "C:\Users\Matt\Desktop\MattExcelFile.xls"
rowVariable = Excel.Application.WorksheetFunction.CountA(Workbooks(xlFilePath).Sheets("Sheet1").Range("C1:C500"))
Debug.Print rowVariable
End Sub
Private Sub Command0_Click()
ImportDataFromRange
End Sub
And here's the error message I receive when running this code, highlighting the rowVariable = ... line:
Run-time error '9':
Subscript out of range
I am not sure if you set appropriate references therefore here is complete code for you with some comments inside.
Sub test()
Dim rowVariable As String '<--why string?
'1st
Dim xlFilePath
xlFilePath ="C:\Users\Matt\Desktop\MattExcelFile.xls"
'2nd- set refernces
Dim EXL As Object
Set EXL = CreateObject("Excel.Application")
'3rd open worksheet- required
With EXL
.Workbooks.Open xlFilePath
'4th count using WF- set appropriate reference to workbook
'be careful about sheet name- give error '9'
Dim tmpName() As String
tmpName = Split(xlFilePath, "\")
rowVariable = .WorksheetFunction.CountA(.Workbooks(tmpName(UBound(tmpName))).Sheets("Arkusz1").Range("A2:A10"))
'control of value
Debug.Print rowVariable
End With
'5th-close everything and clean
EXL.Quit
Set EXL = Nothing
End Sub

LotusScript Function Doesn't Save Form Fields

Can anyone spot an obvious reason as to why my save function isn't saving the fields in my form? It saves the document, but the fields are empty when I open it up. The following code is what I'm using:
Public Sub co_loopNamesAndSaveDocs()
'Dim variables
Dim s As New NotesSession
Dim thisDatabase As NotesDatabase
Set thisDatabase = s.CurrentDatabase
Dim ws As New NotesUIWorkspace
Dim uidoc As NotesUIDocument
Set uidoc = ws.CurrentDocument
Dim currentDoc As NotesDocument
Set currentDoc = uidoc.Document
Dim newDoc As NotesDocument
Dim PersonNameField As NotesItem
Set PersonNameField = currentDoc.GetFirstItem("PersonName")
'Loop through values in PersonNameField and create a new document for each value found
Forall pName In PersonNameField.Values
Set newDoc = New NotesDocument (thisDatabase)
newDoc.Form="newLocationForm"
newDoc.StartDate = currentDoc.StartDate(0)
newDoc.EndDate = currentDoc.EndDate(0)
newDoc.Duration = currentDoc.Duration(0)
newDoc.StartTime = currentDoc.StartTime(0)
newDoc.EndTime = currentDoc.EndTime(0)
newDoc.Comments = currentDoc.Comments(0)
newDoc.Status = currentDoc.Status(0)
newDoc.LocationCode = currentDoc.LocationCode(0)
newDoc.PersonName = pName
Call newDoc.Save (True, False, False)
End Forall
End Sub
Thanks in advance.
Since I don't see an obvious error in the coding, I'd say that the fields in newDoc are blank because the fields in currentDoc are blank. And since currentDoc was set to uidoc.Document, that probably means that you have a synch problem between front-end and back-end documents. I.e., the values exist in your uidoc, but have not yet been saved to the back-end prior to calling this code. If I'm right, try calling uidoc.save() before assigning currentDoc. If you don't want to save to the back-end, then instead of using the back-end as your data source you should be using uidoc.fieldGetText("PersonName") and parsing out the values.

How do I convert an incoming object to binary in Access 2007 VBA?

I need to import PDF and XLS objects and convert them to binary. The following code doesn't work, apparently because the "fd.SelectedItems(1)" is the path of the object instead of the object itself.
If I dim "fileToUpload" as an object, I get a "run time '91' Object variable or With block variable not set".
If I don't specifically Dim "fileToUpload", when I get to the last line below I get "run time '424' Object required".
Anyone know the magic words?
Dim fd As FileDialog
Dim ImageToBytes() As Byte
Dim ImageCode As String
FilePickerControl = False
Set fd = Application.FileDialog(msoFileDialogFilePicker)
fd.AllowMultiSelect = False
If fd.Show = -1 Then
FileToUpload = fd.SelectedItems(1)
End If
ImageToBytes = System.IO.File.ReadAllBytes(FileToUpload)
What is FileToUpload supposed to be? If you dim it as an object, you need to SET it, i.e., SET FileToUpload = fd.SelectedItems(1), but I don't think that's necessarily thecorrect thing, as so far as I can tell, fd.SelectedItems(1) is going to return a string value that has the file name/path, not an actual file object. Looks to me like it should be a string.
Perhaps this will work:
Dim fd As FileDialog
Dim strFileToUpload As String
Dim ImageToBytes() As Byte
Dim ImageCode As String
FilePickerControl = False
Set fd = Application.FileDialog(msoFileDialogFilePicker)
fd.AllowMultiSelect = False
If fd.Show = -1 Then
strFileToUpload = fd.SelectedItems(1)
End If
ImageToBytes = System.IO.File.ReadAllBytes(strFileToUpload)