VBA code when refering a SubReport from a Main Report in Access - vba

I am setting an instruction (VBA) in On Load Event on a report in MS Access.
When I open the report,the code works perfect.
But when i try to embeded the report as a subreport on a main report, the code doesnt work.
I think the problem is that I should be referering the field different(Me.Service1...), since I am trying to call the field now from a main report, but i havent found the right syntaxis.
This is the code i wanna embeed on my main report:
Private Sub Report_Load()
If Me.Service1 = "Scanmachine" Then
Me.Vessel.Visible = True
Me.Label400.Visible = True
Else
Me.Vessel.Visible = False
Me.Label400.Visible = False
End If
End Sub
Any suggestions?

Indeed, you need to change the relative reference of your sub report controls. Over the course of my Access database development work, I have used this Access MVPs resource, even bookmarked the webpage (though it uses forms, the same naming setup applies to reports).
Consider the following, adjusting names accordingly and run this on the main report's OnOpen() event:
Private Sub Report_Load()
If Me![subreportcontrolname].Report!Service1 = "Scanmachine" Then
Me![subreportcontrolname].Report!Vessel.Visible = True
Me![subreportcontrolname].Report!Label400.Visible = True
Else
Me![subreportcontrolname].Report!Vessel.Visible = False
Me![subreportcontrolname].Report!Label400.Visible = False
End If
End Sub

Related

Show/Hide FileExplorer in Access Form

I've been trying to use a combobox to show/hide a PDF viewer that I've added into a MS Access form.
When I use the form_current event, then the form only updates when I move between the data entries. When I use the afterupdate event, the same code does nothing at all.
Does anyone have a fix? The code I have used is below, which I have tried both the AfterUpdate event for the Browser and the Form_Current event for the whole form
Private Sub PDFT900_AfterUpdate() / Private Sub Form_Current()
Dim ESNComb As String
ESNComb = Me.ESNCombo.Column(1)
If ESNComb Like "9????" Then
Me.PDFT900.Visible = True
Else
Me.PDFT900.Visible = False
End If
End Sub
In the code below, I'm hiding and showing the Adobe PDF Reader ActiveX control named, "AcroPDF0". Since the Like operator returns true on an expression match and false on a mismatch or no match, it serves as a simple boolean switch for the visible property. I've used the (*) wild card instead of (?). It works {shrug}. See demonstration images below.
Private Sub ESNCombo_AfterUpdate()
'AcroPDF0.Visible = ESNCombo.Text Like "P*"
AcroPDF0.Visible = ESNCombo.Column(0) Like "P*"
AcroPDF0.src = acroPDFOSrc
End Sub
ComboBox List Items
"File Browser" Selected in ComboBox
Toggled ComboBox back to "PDFT900"

MS Access vba code error with visible invisible VBA code

I have a filed called application. I have written code in vba to make the other field "application_txt" field visible and invisible if a particular value from application field is selected. But for this particulat field application when I write the below code in vba it throws the error. I dont understand why. It works fine for the other fields. Below is the code:
Can anyone please help me with this problem. Is it allowed to give application as field name in MS Access.
Many thanks,
Kiran Chapidi
This is not working:
Private Sub application_Click()
If application.Value = "5= others" Then
Me.application_txt.Visible = True
Else
Me.application_txt.Visible = False
End If
End Sub
This is working fine:
Private Sub Design_Click()
If Design.Value = "3= others" Then
Me.design_txt.Visible = True
Else
Me.design_txt.Visible = False
End If
End Sub

How to check for when a checkbox is checked in a Userform?

I have a Userform that has a Checkbox. I am able to check the value of it, but it is always False whether it is checked or not.
Update
This is how the UserForm is being called (This is in another UserForm ):
Private Sub AddOutgoingbtn_Click()
With New AddIncomingForm
.TopBottom.Value = False
.Show
.Repaint
End With
End Sub
End Update
I created a sub to look for a change in the value like:
Sub TopBottom2_Change()
With AddOutgoingForm
If .TopBottom2.Value = True Then TopBottom = True
If .TopBottom2.Value = False Then TopBottom = False
End With
End Sub
But no matter what I do the .TopBottom2.Value is always False.
I've put a breakpoint on the With line so that I know it is hitting this Sub, then I step through it each time. I open the UserForm and check the box, step through and the value is False, then I uncheck the box and step through. The value is still False.
I am not setting the value in any other way with VBA. I am checking the value in the UserForms code, not in any other place.
I have an If in a Calculation Module that is looking at this value for when it is true or falses, but it is always false.
Here are all the Properties of the Checkbox:
With AddOutgoingForm
That's referring to the form's default instance, which may or may not be the instance that's currently being displayed.
You have two options:
At the call site, instead of doing this (or something similar):
With New AddOutgoingForm
.Show
'...
End With
Do this:
AddOutgoingForm.Show
'...
That way you'll be working with the default instance and the checkbox value-check should work.
...but IMO that's a very very bad idea, because then your form contains code that will only ever work when you're showing the default instance.
Leave the call site alone, and NEVER refer to the default instance of a UserForm inside that form's code-behind. In other words change With AddOutgoingForm for With Me.
The Me keyword refers to the current instance - and that is what you want. Doing this will make the form work regardless of what the call site does.
Alternatively, just drop the With block altogether: With Me wouldn't be doing anything useful here.
I will assume the checkbox is indeed on your user form so just use me or you could use
AddOutgoingForm.TopBottom2.Value
But let me ask you, where is TopBottom and what is it a Boolean variable or another Checkbox? Also on the same form? The Subs are also all in the form? You have to be calling the form from somewhere so be careful between the worksheet, workbook, module variables. Is TopBottom a Global variable to the whole project (in a module called Global_Variables with Public in front of it perhaps?) You may not have access to TopBottom from inside of your form if you are not passing anything in or out.
Private Sub TopBottom2_Change()
If (Me.TopBottom2.Value) = True Then
Me.TopBottom.Value = True
Else
Me.TopBottom.Value = False
End If
End Sub
Cheers,
-WWC

How to use a checkbox to change values in an SQL database in asp.net

I currently have system which has a web front end and a back office system. User can book properties online or call our office to book a property. In the admin system on the web front, I have two check boxes to determine if the property is available on the front end or the back office system. This is controlled using a Check box. The code for the check box is as follows;
<asp:CheckBox ID="CheckBoxAvailableToWeb" runat="server" TextAlign="Left" Text="Available for web bookings"
Checked="true" />
I have an field in an SQL Database called "isAvailableToWeb" which has a Boolean result. What I want to achieve is if the check box is checked, the value of the "isAvailableToWeb" field is set to "True" or set to "False" if un-checked.
I have tried to complete this function using the following code;
Protected Sub CheckBoxAvailableToWeb_CheckedChanged(sender As Object, e As EventArgs, ByVal beachhutid As Long)
Using dbContext = New bbhasDBEntities
Dim item
item = (From i In dbContext.tblBeachHuts Where i.beachHutId = beachhutid Select i).First()
If CheckBoxAvailableToWeb.Checked = True Then
item.AvailableToWeb = True
Else
item.AvailableToWeb = False
End If
dbContext.tblBeachHuts.Attach(item)
Call dbContext.SaveChanges()
End Using
End Sub
This code doesn't throw up any errors but doesn't also make the change that I would like to see.
I have a button on this page that saves the information so I would also like to know if it would be better, once the code is working, to put it in that Sub.
You need to call dbContext.SaveChanges to persist to the database (assuming bbhasDBEntities is an instance of DbContext), assign to the entity rather than the boolean and call First() to get the first matching element rather than a collection.
Protected Sub CheckBoxAvailableToWeb_CheckedChanged(sender As Object, e As EventArgs, ByVal beachhutid As Long)
Using dbContext = New bbhasDBEntities
Dim item
item = (From i In dbContext.tblBeachHuts Where i.beachHutId = beachhutid Select i).First()
If CheckBoxAvailableToWeb.Checked = True Then
item.AvailableToWeb = True
Else
item.AvailableToWeb = False
End If
Call dbContext.SaveChanges()
End Using
End Sub
Try using 1 and 0 instead of true and false when assigning values into AvailableToWeb.
I use this method with a dataset and it works correctly.

VB 2013 Application Out of Memory

I'm new to VB but recently created my first working app :) Anyway it just compresses files and little bit more. The latest thing that I added was a marquee style progress bar to animate while the operation was in progress and stop when it ends and the user can do the next zip operation. The progress bar wasn't updating, so I used a background worker to do the actual task while the button click just did the animation. Since then I've notcied serious degredation in the app. It struggles to load. I even got an out of memory error. Not sure if the background worker is related, but I thought I'd mention as it was the last update. Has anyone experienced anything similar? If I can provide and specific info, please ask me for it! Many thanks.
UPDATE: So I understand that I'm not using the BGWorker correctly. I will change that. But I found even with that removed, I still had issues. So I created a new form and started adding in bits of my code one by one. Anyway, I fell at the first hurdle with my form load sub. So I added that in slowly. I found that when ever I have any statements that load a variable from settings for persistent settings that the app falls over. Below is my code. Can anyone see what's up?????
UPDATE: I've found that if I load from settings the memory useage shoots up. I tried this too with saving settins on form closed. Below is the error received. The same out of memory occurs when trying to load settings too. I never experienced this on the first form I created. So perhaps I have missed some settings on the second, because the implementation in the code hasn't changed.
System.Configuration.ConfigurationErrorsException: Failed to save settings: An error occurred executing the configuration section handler for userSettings/Backup_Tool.My.MySettings. ---> System.Configuration.ConfigurationErrorsException: An error occurred executing the configuration section handler for userSettings/Backup_Tool.My.MySettings. ---> System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown
This is when I added in the code below:
Private Sub Form1_Closed(sender As Object, e As EventArgs) Handles MyBase.FormClosed
' TAB PAGE 1.
' Save controls to settings.
My.Settings.StartPathTextBox1 = StartPathTextBox1.Text
My.Settings.ZipPathTextBox1 = ZipPathTextBox1.Text
My.Settings.CopyPathTextBox1 = CopyPathTextBox1.Text
My.Settings.ZipSelectCheckBox1 = ZipSelectCheckBox1.Checked
My.Settings.CopySelectCheckBox1 = CopySelectCheckBox1.Checked
For Each s As String In StartNameListBox1.Items()
My.Settings.StartNameListBoxItems1.Add(s)
Next
For Each s As String In StartNameListBox1.SelectedItems()
My.Settings.StartNameListBoxSelectedItems1.Add(s)
Next
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' FORM 1.
' Initialise specialised string collections.
If My.Settings.StartNameListBoxItems1 Is Nothing Then
My.Settings.StartNameListBoxItems1 = _
New System.Collections.Specialized.StringCollection
End If
If My.Settings.StartNameListBoxSelectedItems1 Is Nothing Then
My.Settings.StartNameListBoxSelectedItems1 = _
New System.Collections.Specialized.StringCollection
End If
' TAB PAGE 1.
' Restore controls from saved settings.
StartPathTextBox1.Text() = My.Settings.StartPathTextBox1
ZipPathTextBox1.Text() = My.Settings.ZipPathTextBox1
CopyPathTextBox1.Text() = My.Settings.CopyPathTextBox1
ZipSelectCheckBox1.Checked = My.Settings.ZipSelectCheckBox1
CopySelectCheckBox1.Checked = My.Settings.CopySelectCheckBox1
For Each s As String In My.Settings.StartNameListBoxItems1()
StartNameListBox1.Items.Add(s)
Next
For Each s As String In My.Settings.StartNameListBoxSelectedItems1()
StartNameListBox1.SelectedItems.Add(s)
Next
' Decide controls initial states.
If StartNameListBox1.SelectedItems.Count = 0 Then
ZipSelectCheckBox1.Enabled = False
RunButton1.Enabled = False
End If
If ZipSelectCheckBox1.Checked = False Then
ZipPathTextBox1.Enabled = False
ZipBrowseButton1.Enabled = False
End If
If ZipPathTextBox1.Text = String.Empty Then
CopySelectCheckBox1.Enabled = False
End If
If CopySelectCheckBox1.Checked = False Then
CopyPathTextBox1.Enabled = False
CopyBrowseButton1.Enabled = False
End If
End Sub
It appears to be that you are only ever adding the current selections to the Settings collections. You might well clear the ListBox when they make new selections, but you do not do the same thing with the Settings Collections like My.Settings.StartNameListBoxItems1:
For Each s As String In StartNameListBox1.Items()
My.Settings.StartNameListBoxItems1.Add(s)
Next
The collection will have all the items in it from all the other times it has ever run already and you are now going to add more to it. Eventually you will have many, many, many items in it.
My.Settings.StartNameListBoxItems1.Clear ' REMOVE ALL OLD ITEMS
' Save just the current items to the collection
For Each s As String In StartNameListBox1.Items()
My.Settings.StartNameListBoxItems1.Add(s)
Next
use .Clear on both Collections