I'm trying to create a sub that will toggle the visibility of a given array of controls in a given form location. I've managed to make it work on a main form, but can't figure out the syntax for a subform control. I'm working with Access 2016.
Here's what I have so far:
Sub toggleDisappear(ByRef fields() As Variant, _
ByVal report As String, ByVal vis As Boolean, Optional ByVal sfrm As String)
If IsNull(sfrm) Then
For i = 1 To UBound(fields)
Forms(report).Controls(fields(i)).Visible = vis
Next
Else
For i = 1 To UBound(fields)
Forms.Form(report).Controls(sfrm).Form.Controls(fields(i)).Visible = vis
Next
End If
End Sub
Any ideas what I'm missing? I get a run-time error 438 - "Object doesn't support this property or method."
If you type sFrm as String in VBA, it can never be null. Therefore your If statement will not work properly. To fix:
Change your procedure declaration to:
Sub toggleDisappear(ByRef fields() As Variant, _
ByVal report As String, ByVal vis As Boolean, Optional ByVal sfrm As Variant)
Then, change your If statement to:
If IsMissing(sfrm) Then
Now your code can tell when sFrm is not supplied.
Related
I designed code for a GUI grade Calculator but now I am being asked to add 1. create a function that will contain all of your data validation code. The function should return true if all data checks have passed or a false if any of the data is invalid. If the data is valid (true returned), add the number to the list box, if it is not valid (false returned), show an error message.2)Add a Sub procedure that will reset the form fields back to their initial state. Call this sub procedure when the clear button is clicked.
2.add a Sub procedure that will reset the form fields back to their initial state. Call this sub procedure when the clear button is clicked.
create a function that will count the 90s in the listbox. Pass the listbox in a a parameter and loop the contents to count each of the 90s in the listbox. Return the count of the 90s in the list box to the caller. Use it as input in the re-write eligibility check.
This is my Code.
Public Class Form1
Private Sub BuGrade_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BuGrade.Click
gradeList.Items.Add(txtgrade.Text)
txtgrade.Clear()
End Sub
Private Sub BuAverage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BuAverage.Click
Dim total As Double
Dim avg As Double
Dim grade As Double
Dim gradecount As Integer
For gradecount = 0 To gradeList.Items.Count - 1
grade = gradeList.Items(gradecount)
total += grade
Next
avg = total / gradeList.Items.Count
lblR.Text = "The final total score is " & total & vbCrLf & "The Average score is " & avg
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
lblR.Text = String.Empty
End Sub
End Class
I'm in the process of creating a database with many chemicals & associated .pdf files with their information.
Each chemical has a unique ID and in the same row has a link to the .pdf file on the network.
In addition, each chemical has a location assigned to it.
My goal is to be able to print all .pdf associated with chemicals in each location.
For example:
ID Chemical Location PDF-link
1 Acetone Lab-A A:/folder/1.pdf
2 Fire Lab-A A:/folder/2.pdf
1 Acetone Lab-B A:/folder/1.pdf
3 Sponge Lab-B A:/folder/3.pdf
4 Candy Lab-B A:/folder/4.pdf
If I specify Lab-A, I would like it to print both PDFs: 1.pdf, 2.pdf.
If I specify Lab-B, I would like it to print those respective .pdf files.
Of course I also want to be able to print all of them as well, but I think if I figure out how to do the above, I can manage to do this.
Thanks for any help.
The code to do this will look like this:
Sub PrintMyPdf()
Dim rst As DAO.Recordset
Dim strSQL As String
strSQL = "select * from tblChem where Location = '" & Me.txtLocation & "'"
Set rst = CurrentDb.OpenRecordset(strSQL)
Do While rst.EOF = -False
Call PrintOnePdf(rst![PDF-link])
rst.MoveNext
Loop
rst.Close
End Sub
Sub PrintOnePdf(strF As String)
CreateObject("Shell.Application").Namespace(0).ParseName(strF).InvokeVerb ("Print")
End Sub
The above code assumes you have a text box on the form of txtLocation, and then the above code can be placed behind a button click even (or simply call the above code from the button click even.
The above code assumes you have some type of PDF reader for the printing to occur.
Have a look at this: http://www.jpsoftwaretech.com/open-or-print-files-in-vba/
I have used it successfully by putting a command button on a form e.g:
In a module I put this:
Public Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
(ByVal hWnd As Long, ByVal lpOperation As String, ByVal lpFile As String, _
ByVal lpParameters As String, ByVal lpDirectory As String, _
ByVal nShowCmd As Long) As Long
The OnClick event for one of my command buttons is:
PathName1 = "Full path.pdf"
PathName2 = "Full path.rtf"
ExecuteFile PathName1, printfile
ExecuteFile PathName2, printfile
You'll need to set up a Select Case or If Then Else statement to print what you actually want.
Hello i generate HTML input text controls with a string builder and put it inside a div from Code Behind.
Then i also need to assign values to these generated input & update database if values change by the user.
The problem is Code Behind can't find the generated from String builder HTML Input-text Controls
You can see code example below:
Public Class WebForm1
Inherits System.Web.UI.Page
Private Sub WebForm1_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
Dim strB As New StringBuilder
For i = 0 To 5
strB.Append("<input type=""text"" value="""" runat=""server"" id=position_" & i & "/>")
Next
wraper.InnerHtml = strB.ToString
strB.Clear()
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
For i = 0 To 5
Dim a As HtmlInputText
a = Me.Page.FindControl("position_" & i)
a.Value = "test"
Next
End Sub
End Class
That's because you're not creating controls, you're creating strings without a context.
You might want to look at these questions:
ASP: runat=server for dynamic control
Dynamically Created Controls losing data after postback
I'm trying to develop a sub that I can pass different variables to save me some time when creating quite a few different types of listbox in VBA Access. I've come up with the following:
Public Sub openCASEFORM(f As Form, list As ListBox)
DoCmd.OpenForm f, , , "[FileID]=" & f.list
End Sub
And I call it as follows:
Private Sub listPreAn_Click()
Call openCASEFORM(Me, listPreAn)
End Sub
However when trying it I get a Run-time error '2465' Application-defined or object-defined error. I'm struggling to see where it's going wrong. Any thoughts?
What happens if you use
Call openCaseFORM(Me, Me!listPreAn
?
Well of if you do not use list as Listbox as second parameter, You are not using it anyway.
Your code is really badly written, as you're passing objects but you need to use strings to do the job. I'd rewrite it thus:
Public Sub openCASEFORM(f As String, list As String)
DoCmd.OpenForm f, , , "[FileID]=" & f(list)
End Sub
Either that, or:
Public Sub openCASEFORM(f As String, list As ListBOx)
DoCmd.OpenForm f, , , "[FileID]=" & list.Value
End Sub
Both of these assume the listbox is a simple one (i.e., not multiselect).
I would suggest that it would make more sense to rewrite thus:
Public Sub openCASEFORM(ByVal strFormName As String, ByVal strCriteria As String)
DoCmd.OpenForm strFormName, , , strCriteria
End Sub
That way, you could pass any arbitrary WHERE clause you liked.
I'm using ADO 2.1 in MS Access 2003 and calling the AddNew method of an ADO recordset using an array of field names and an array of values. I am getting no error messages however, the record is not being written to the table.
I have tried following the command with a .Update and a .Requery to no avail.
Any ideas?
Public Function ReadFileViaTextStream(ByVal PortfolioName As String, ByVal SourceFile As String, ByVal TargetTable As String, _
ByRef TargetFields(), ParamArray SourceFields() As Variant)
Dim p_adoRS As ADODB.Recordset
Dim ForWriting() As Variant
Set p_adoRS = New ADODB.Recordset
p_adoRS.Open TargetTable, CurrentProject.Connection, adOpenDynamic, adLockBatchOptimistic, adCmdTable
If p_adoRS.Supports(adAddNew) Then
p_adoRS.AddNew TargetFields(), ForWriting()
p_adoRS.Update
p_adoRS.Requery
End If
TargetTable is a string parameter, TargetFields is an array of field names and ForWriting is an array of values.
Right - Myself and a colleague have found the issue. The recordset should be opened as adLockOptimistic, not adLockBatchOptimistic.