Hiya guys, im kinda new to posting here even though ive been lurking for awhile..reading up on topics that had helped me in building a database for my school.
But now i need further help.
To tell you guys frankly i suck big time on VB and my coding is in such a dire mess. Im looking for some help in optimizing my code below...perhaps a subroutine or two...i tried to do it on my own, but ive messed up still. Heh. -
Private Sub chkSecOne_AfterUpdate()
-
If Me.chkSecOne = True Then
-
Me.txtSec11.Enabled = True
-
Me.txtSec12.Enabled = True
-
Me.txtSec13.Enabled = True
-
Me.txtSec14.Enabled = True
-
Me.txtSec15.Enabled = True
-
Me.txtSec1A.Enabled = True
-
Me.txtSec1B.Enabled = True
-
Me.txtSec1C.Enabled = True
-
Else
-
Me.txtSec11.Enabled = False
-
Me.txtSec12.Enabled = False
-
Me.txtSec13.Enabled = False
-
Me.txtSec14.Enabled = False
-
Me.txtSec15.Enabled = False
-
Me.txtSec1A.Enabled = False
-
Me.txtSec1B.Enabled = False
-
Me.txtSec1C.Enabled = False
-
End If
-
End Sub
-
Private Sub chkSecTwo_AfterUpdate()
-
If Me.chkSecTwo = True Then
-
Me.txtSec21.Enabled = True
-
Me.txtSec22.Enabled = True
-
Me.txtSec23.Enabled = True
-
Me.txtSec24.Enabled = True
-
Me.txtSec25.Enabled = True
-
Me.txtSec2A.Enabled = True
-
Me.txtSec2B.Enabled = True
-
Me.txtSec2C.Enabled = True
-
Else
-
Me.txtSec21.Enabled = False
-
Me.txtSec22.Enabled = False
-
Me.txtSec23.Enabled = False
-
Me.txtSec24.Enabled = False
-
Me.txtSec25.Enabled = False
-
Me.txtSec2A.Enabled = False
-
Me.txtSec2B.Enabled = False
-
Me.txtSec2C.Enabled = False
-
End If
-
End Sub
-
Private Sub chkSecThree_AfterUpdate()
-
If Me.chkSecThree = True Then
-
Me.txtSec31.Enabled = True
-
Me.txtSec32.Enabled = True
-
Me.txtSec33.Enabled = True
-
Me.txtSec34.Enabled = True
-
Me.txtSec35.Enabled = True
-
Me.txtSec3A.Enabled = True
-
Me.txtSec3B.Enabled = True
-
Me.txtSec3C.Enabled = True
-
Else
-
Me.txtSec31.Enabled = False
-
Me.txtSec32.Enabled = False
-
Me.txtSec33.Enabled = False
-
Me.txtSec34.Enabled = False
-
Me.txtSec35.Enabled = False
-
Me.txtSec3A.Enabled = False
-
Me.txtSec3B.Enabled = False
-
Me.txtSec3C.Enabled = False
-
End If
-
End Sub
-
Private Sub chkSecFour_AfterUpdate()
-
If Me.chkSecFour = True Then
-
Me.txtSec41.Enabled = True
-
Me.txtSec42.Enabled = True
-
Me.txtSec43.Enabled = True
-
Me.txtSec44.Enabled = True
-
Me.txtSec45.Enabled = True
-
Me.txtSec4A.Enabled = True
-
Me.txtSec4B.Enabled = True
-
Me.txtSec4C.Enabled = True
-
Else
-
Me.txtSec41.Enabled = False
-
Me.txtSec42.Enabled = False
-
Me.txtSec43.Enabled = False
-
Me.txtSec44.Enabled = False
-
Me.txtSec45.Enabled = False
-
Me.txtSec4A.Enabled = False
-
Me.txtSec4B.Enabled = False
-
Me.txtSec4C.Enabled = False
-
End If
-
End Sub
-
Im basically trying to disable the controls (txtSec11, txtSec12, etc..) depending if the user clicks on the checkbox (chkSecOne, chkSecTwo, etc..). This works fine...except that as you guys can see...the code is pretty damn long and repeatable.
So i hope u folks out there can help me with an example of a subroutine or something.
Thankss!!!
13 1959 PEB 1,418
Expert 1GB
U can do sth like this:
Me.txtSec21.Enabled = Me.chkSecTwo
Me.txtSec22.Enabled = Me.chkSecTwo
Me.txtSec23.Enabled = Me.chkSecTwo
Me.txtSec24.Enabled = Me.chkSecTwo
Me.txtSec25.Enabled = Me.chkSecTwo
Me.txtSec2A.Enabled = Me.chkSecTwo
Me.txtSec2B.Enabled = Me.chkSecTwo
Me.txtSec2C.Enabled = Me.chkSecTwo
I've got your second section... Instaed writting if, directly put the value to the respective control!
If not for each control u need a separate line... At least one!
If u don't like this... u need to but the data for ur controls in a database and than cycle the database setting the respective values! But this is long and this doesn't matter the efforts!
Marry Cristmas!
Hiya guys, im kinda new to posting here even though ive been lurking for awhile..reading up on topics that had helped me in building a database for my school.
But now i need further help.
To tell you guys frankly i suck big time on VB and my coding is in such a dire mess. Im looking for some help in optimizing my code below...perhaps a subroutine or two...i tried to do it on my own, but ive messed up still. Heh. -
Private Sub chkSecOne_AfterUpdate()
-
If Me.chkSecOne = True Then
-
Me.txtSec11.Enabled = True
-
Me.txtSec12.Enabled = True
-
Me.txtSec13.Enabled = True
-
Me.txtSec14.Enabled = True
-
Me.txtSec15.Enabled = True
-
Me.txtSec1A.Enabled = True
-
Me.txtSec1B.Enabled = True
-
Me.txtSec1C.Enabled = True
-
Else
-
Me.txtSec11.Enabled = False
-
Me.txtSec12.Enabled = False
-
Me.txtSec13.Enabled = False
-
Me.txtSec14.Enabled = False
-
Me.txtSec15.Enabled = False
-
Me.txtSec1A.Enabled = False
-
Me.txtSec1B.Enabled = False
-
Me.txtSec1C.Enabled = False
-
End If
-
End Sub
-
Private Sub chkSecTwo_AfterUpdate()
-
If Me.chkSecTwo = True Then
-
Me.txtSec21.Enabled = True
-
Me.txtSec22.Enabled = True
-
Me.txtSec23.Enabled = True
-
Me.txtSec24.Enabled = True
-
Me.txtSec25.Enabled = True
-
Me.txtSec2A.Enabled = True
-
Me.txtSec2B.Enabled = True
-
Me.txtSec2C.Enabled = True
-
Else
-
Me.txtSec21.Enabled = False
-
Me.txtSec22.Enabled = False
-
Me.txtSec23.Enabled = False
-
Me.txtSec24.Enabled = False
-
Me.txtSec25.Enabled = False
-
Me.txtSec2A.Enabled = False
-
Me.txtSec2B.Enabled = False
-
Me.txtSec2C.Enabled = False
-
End If
-
End Sub
-
Private Sub chkSecThree_AfterUpdate()
-
If Me.chkSecThree = True Then
-
Me.txtSec31.Enabled = True
-
Me.txtSec32.Enabled = True
-
Me.txtSec33.Enabled = True
-
Me.txtSec34.Enabled = True
-
Me.txtSec35.Enabled = True
-
Me.txtSec3A.Enabled = True
-
Me.txtSec3B.Enabled = True
-
Me.txtSec3C.Enabled = True
-
Else
-
Me.txtSec31.Enabled = False
-
Me.txtSec32.Enabled = False
-
Me.txtSec33.Enabled = False
-
Me.txtSec34.Enabled = False
-
Me.txtSec35.Enabled = False
-
Me.txtSec3A.Enabled = False
-
Me.txtSec3B.Enabled = False
-
Me.txtSec3C.Enabled = False
-
End If
-
End Sub
-
Private Sub chkSecFour_AfterUpdate()
-
If Me.chkSecFour = True Then
-
Me.txtSec41.Enabled = True
-
Me.txtSec42.Enabled = True
-
Me.txtSec43.Enabled = True
-
Me.txtSec44.Enabled = True
-
Me.txtSec45.Enabled = True
-
Me.txtSec4A.Enabled = True
-
Me.txtSec4B.Enabled = True
-
Me.txtSec4C.Enabled = True
-
Else
-
Me.txtSec41.Enabled = False
-
Me.txtSec42.Enabled = False
-
Me.txtSec43.Enabled = False
-
Me.txtSec44.Enabled = False
-
Me.txtSec45.Enabled = False
-
Me.txtSec4A.Enabled = False
-
Me.txtSec4B.Enabled = False
-
Me.txtSec4C.Enabled = False
-
End If
-
End Sub
-
Im basically trying to disable the controls (txtSec11, txtSec12, etc..) depending if the user clicks on the checkbox (chkSecOne, chkSecTwo, etc..). This works fine...except that as you guys can see...the code is pretty damn long and repeatable.
So i hope u folks out there can help me with an example of a subroutine or something.
Thankss!!!
I've only done this sort of thing in VB6, so it may be different. But perhaps you could put each group of controls in an option group, or form, or whatever it's called (I forget). Then just enable/disable that container rather than each of the controls.
okie...thanks for the promt replies guys.
i will try it out definitely.
cheers!
I've only done this sort of thing in VB6, so it may be different. But perhaps you could put each group of controls in an option group, or form, or whatever it's called (I forget). Then just enable/disable that container rather than each of the controls.
Sorry, I mean to say frame there, not form.
This little routine might be fun -
-
Dim ctTmp As Control
-
Dim iText As Integer
-
-
For Each ctTmp In Form1.Controls
-
If ctTmp.ControlType = acTextBox Then
-
iText = CInt(Left(Right(ctTmp.Name, 2), 1))
-
Select Case iText
-
Case 1
-
ctTmp.Enabled = chkSecOne
-
-
Case 2
-
ctTmp.Enabled = chkSecTwo
-
-
Case 3
-
ctTmp.Enabled = chkSecThree
-
-
Case 4
-
ctTmp.Enabled = chkSecFour
-
-
Case Else
-
'that is enough for me
-
-
End Select
-
End If
-
Next 'ctTmp
-
Put it in it's own sub and call it from any event you like. It will only work if there are no other textboxes on your form. You could modify it to exclude any other textboxes by name. Over to you
NeoPa 32,556
Expert Mod 16PB
This isn't the most compression of code you can do (See Will's post for that) but it is probably as close as you'll get without getting quite complicated : - Private Sub chkSecOne_AfterUpdate()
-
With Me.chkSecOne
-
Me.txtSec11.Enabled = .Value
-
Me.txtSec12.Enabled = .Value
-
Me.txtSec13.Enabled = .Value
-
Me.txtSec14.Enabled = .Value
-
Me.txtSec15.Enabled = .Value
-
Me.txtSec1A.Enabled = .Value
-
Me.txtSec1B.Enabled = .Value
-
Me.txtSec1C.Enabled = .Value
-
End With
-
End Sub
-
Private Sub chkSecTwo_AfterUpdate()
-
With Me.chkSecTwo
-
Me.txtSec21.Enabled = .Value
-
Me.txtSec22.Enabled = .Value
-
Me.txtSec23.Enabled = .Value
-
Me.txtSec24.Enabled = .Value
-
Me.txtSec25.Enabled = .Value
-
Me.txtSec2A.Enabled = .Value
-
Me.txtSec2B.Enabled = .Value
-
Me.txtSec2C.Enabled = .Value
-
End With
-
End Sub
-
Private Sub chkSecThree_AfterUpdate()
-
With Me.chkSecThree
-
Me.txtSec31.Enabled = .Value
-
Me.txtSec32.Enabled = .Value
-
Me.txtSec33.Enabled = .Value
-
Me.txtSec34.Enabled = .Value
-
Me.txtSec35.Enabled = .Value
-
Me.txtSec3A.Enabled = .Value
-
Me.txtSec3B.Enabled = .Value
-
Me.txtSec3C.Enabled = .Value
-
End With
-
End Sub
-
Private Sub chkSecFour_AfterUpdate()
-
With Me.chkSecFour
-
Me.txtSec41.Enabled = .Value
-
Me.txtSec42.Enabled = .Value
-
Me.txtSec43.Enabled = .Value
-
Me.txtSec44.Enabled = .Value
-
Me.txtSec45.Enabled = .Value
-
Me.txtSec4A.Enabled = .Value
-
Me.txtSec4B.Enabled = .Value
-
Me.txtSec4C.Enabled = .Value
-
End With
-
End Sub
... - Private Sub chkSecOne_AfterUpdate()
-
With Me.chkSecOne
-
Me.txtSec11.Enabled = .Value
-
Me.txtSec12.Enabled = .Value
-
...
I really wish Access supported control arrays, as they would make things like this one heck of a lot simpler.
Willakawill pointed out a document at Microsoft describing a way of simulating them, but I'm not very happy with it. So I was wondering, couldn't you create an array at form level and set the elements to "point to" (I forget the proper term) the ones on the form?
Here's a little trial I knocked up, which appeared to work. Unfortunately, trying to test it (in the Click event) I ran into this stupid restriction in Access where you can't seem to do anything with a control unless it has the focus :(. But presumably this would apply no matter how you reference the controls, so is not really relevant here.
If anyone would like to try this, just create a new form, add a couple of unbound text boxes using the default names Text0, Text1 etc. Then get to the code editor for the form and put in this code (also, I think in Access unlike "real" VB, you have to explicitly tell the form to look for these events by attaching [Event Procedure] to the event). This will set up an array and link the elements to them. - Option Compare Database
-
Option Explicit
-
-
Private MyTextBox() As TextBox
-
Private MyTextBoxCount As Long
-
-
Private Sub Form_Load()
-
Dim I As Long
-
Dim ctl As Control
-
For Each ctl In Me.Controls
-
If TypeOf ctl Is TextBox Then
-
If Left$(ctl.Name, 4) = "Text" Then
-
MyTextBoxCount = MyTextBoxCount + 1
-
ReDim Preserve MyTextBox(1 To MyTextBoxCount)
-
Set MyTextBox(MyTextBoxCount) = ctl
-
End If
-
End If
-
Next
-
Set ctl = Nothing
-
End Sub
-
- Private Sub Text0_Change()
-
Dim I As Long
-
Debug.Print MyTextBoxCount
-
For I = 1 To MyTextBoxCount
-
' This line produces an error due to a silly
-
' limitation of VBA...
-
Debug.Print , MyTextBox(I).Text
-
Next
-
End Sub
- Me.txtSec11.Enabled = .Value
-
Me.txtSec12.Enabled = .Value
-
Me.txtSec13.Enabled = .Value
That's another thing. How come this works, when Access won't let you change properties of a control that doesn't have the focus? What am I missing here? The rules for this "must have focus" limitation must be documented somewhere - I'd appreciate it if someone could point me toward it.
(Note, in my prior post, the error happened when accessing the second element of the array - the first worked alright, because Text0 did have the focus. So the basic idea seems sound.)
That's another thing. How come this works, when Access won't let you change properties of a control that doesn't have the focus? What am I missing here? The rules for this "must have focus" limitation must be documented somewhere - I'd appreciate it if someone could point me toward it.
(Note, in my prior post, the error happened when accessing the second element of the array - the first worked alright, because Text0 did have the focus. So the basic idea seems sound.)
The answer to all these problems is to run access as the back end and vb as the front end. I can't stand bound controls so I was never seduced by the 'wizardry' of access. I have found everything so much easier unbound and in code. This means I can port my code design to several languages
The answer to all these problems is to run access as the back end and vb as the front end. I can't stand bound controls so I was never seduced by the 'wizardry' of access. I have found everything so much easier unbound and in code. This means I can port my code design to several languages
The thing is, where I work, everyone has Access (at least runtime) and almost nobody has VB. If I throw together a quick and simple utility using an Access database, it's often simpler to just do it in Access.
For anything serious, I'd also prefer VB as the front-end.
(All the same, I'm fairly proud of having invented another way of simulating control arrays. And in my opinion, a better method, in some ways. :))
NeoPa 32,556
Expert Mod 16PB
If I were doing it for myself, I'd use your way Killer (or something very similar), but posting for an inexperienced coder I think something a little more straightforward is required.
If I were doing it for myself, I'd use your way Killer (or something very similar), but posting for an inexperienced coder I think something a little more straightforward is required.
Yeah, probably true.
I might have a think about my simulation thing and see what I can do to make it simpler to implement.
Hi guys...thanks for all the replies....really had been a great help!
Bless u guys....and have a happy new year!!
Cheers!
Sign in to post your reply or Sign up for a free account.
Similar topics
by: joe |
last post by:
Hello:
I am using IIS 5.0, ASP and VBScript. I am puzzled by recurrences of errors
when I nest subroutines, and I'd like to know if there are rules for that
which I don't know. Would functions...
|
by: Koen |
last post by:
Hi all,
I created a function that updates a certain status field. The status can be
influenced by four different other fields. So, on the form where I edit
these fields I call the function (see...
|
by: Microsoft |
last post by:
I'm not sure where to physically place my subroutines in vb.net I get
namespace and not declared errors...
Imports System
Imports System.Management
Public Class Form1
|
by: OpticTygre |
last post by:
I have a class, ProcessFiles, with several subroutines it runs for each type
of file I want to "process."
First, I check directories for files. Then, based on the filenames of those
I find in...
|
by: kmcnet |
last post by:
Hello Everyone and thanks for your help in advance. I have been battling a
problem for nearly a month with the FileSystemWatcher component. Basically,
what I am trying to do it to monitor three...
|
by: Victor |
last post by:
In ASP, if I define a subroutine as private
Private Sub mySubroutine(arg1, arg2)
I understand that the variables I define inside the subroutine are local only to the
subroutine. If the same...
|
by: Wijaya Edward |
last post by:
Hi,
How does one benchmark
multiple subroutines in Python?
Is there a built-in library for that?
--
Regards,
Edward WIJAYA
SINGAPORE
|
by: phuker420 |
last post by:
don't know how to get started please help with this program.
rock,paper,scissors
We will write a program that multiple games of rock, paper, scissors: keeps a cumulitive score, quits when...
|
by: patelxxx |
last post by:
Subroutines: set_asset & destination_filename:
Basically I'm looking at some existing code used to upload images/files, can you help me understand exactly what is happening:
sub set_asset {
...
|
by: liz0001 |
last post by:
Hi,
I need to access an .asp subroutine file from a subdomain.
i.e. the website is at www.domain.com and I want to access the same subroutines on the subdomain mobile.domain.com.
The...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
| |