473,769 Members | 5,131 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dynamically open forms, reports or call functions

Hello,

I'm toying with the idea of loading a MenuStrip (VB.Net 2005)
dynamically based on who is logged into my system. Every user has
different security settings and I want to customize the main menu
instead of showing all possible options and only enabling/disabling
certain ones.

I have a table that stores the menu item name, parent item (if
applicable), display order, etc. so that I can dynamically load my
menu. There's also a column called "FormToOpen ". If that particular
field is populated, I obviously want to open the specified form if the
menu item is clicked. I have a field called 'ReportToOpen'. If that
particular field is populated, I want to open a SQL Server Report
(utilitizing Reporting Services) when the menu item is selected. And
lastly, I have a field called "RoutineToCall" . If that field is
populated I want to call the routine.

I suppose I could add all the menu items, place the appropriate code
behind each item, and then remove items from the menu if they're not
applicable.

I have a couple of questions:

1. Should I scratch what I'm thinking and just go with my second
option above, namely just adding all the items and code at design-time
and just removing unneeded items, or

2. Go about what I'm trying to do. And if so, how? How do I open a
form if the name of the form is stored in a database field. How do I
call a sub routine if the name of the routine is stored in a database
field?

Thanks a lot.

Steve

Apr 14 '06 #1
7 10066
St*********@Eag leCCI.com wrote:
Hello,

I'm toying with the idea of loading a MenuStrip (VB.Net 2005)
dynamically based on who is logged into my system. Every user has
different security settings and I want to customize the main menu
instead of showing all possible options and only enabling/disabling
certain ones.

I have a table that stores the menu item name, parent item (if
applicable), display order, etc. so that I can dynamically load my
menu. There's also a column called "FormToOpen ". If that particular
field is populated, I obviously want to open the specified form if the
menu item is clicked. I have a field called 'ReportToOpen'. If that
particular field is populated, I want to open a SQL Server Report
(utilitizing Reporting Services) when the menu item is selected. And
lastly, I have a field called "RoutineToCall" . If that field is
populated I want to call the routine.

I suppose I could add all the menu items, place the appropriate code
behind each item, and then remove items from the menu if they're not
applicable.

I have a couple of questions:

1. Should I scratch what I'm thinking and just go with my second
option above, namely just adding all the items and code at design-time
and just removing unneeded items, or

2. Go about what I'm trying to do. And if so, how? How do I open a
form if the name of the form is stored in a database field. How do I
call a sub routine if the name of the routine is stored in a database
field?

Thanks a lot.

Steve

You're asking if you should take an extremely complicated approach to a
situation just for the sake of using a database, rather than dynamically
building a menu based on the privileges the user has. The choice is
entirely yours - it is you that will have to live with it. Which one do
you want to maintain? Which one do you want to look at in 6 months to
make changes to?

Tom
Apr 14 '06 #2

Thanks Tom. I think the answer to your question really relies on how
complicated it is to program the dynamic menus. When I say this, I
don't mean actually loading the menu items dynamically. I suspect
that'll be the easy part. I mean opening a form, report or calling a
routine if the name of that form, report or routine is stored in a
variable.

i.e.

--Set Connection properties
--Open datareader
--Load Menu and store the appropriate form, report or routine name to
open in the Tag property of the menu item.
--On click of the menu, open the form or report, or call the routine,
that's named in the Tag property.

Depending on how to do this, I don't necessarily agree with you that
the database approach is extremely complicated. For instance, in
Microsoft Access I would simply do a Docmd.OpenForm (menuitem.Tag) or a
Docmd.OpenRepor t (menuitem.Tag) or an Eval(menuitem.T ag). If I
hardcode the entire menu and then remove items that aren't needed for
particular users, every time I add new functionality I'll always have
to remember to update the menu instead of simply adding another record
to a table. Also, if I'm delivering a new SQL Server report then my
code may not change at all - I just have to add another record to my
table and deliver the report to my clients. If I hardcode the entire
menu and then remove menu items that aren't needed, I'll have to update
the menu every time I add a report and that means I'll have to
re-deploy the application.

Perhaps this is the way I should have asked these questions:

How can I open a form if the name of the form is stored in a variable?
How can I call a routine if the name of the routine is stored in
variable?

I won't ask about opening a SQL Server report...If I can get those two
questions I can figure out this one easily enough.

Thanks again,

Steve

Apr 14 '06 #3
St*********@Eag leCCI.com wrote:
Thanks Tom. I think the answer to your question really relies on how
complicated it is to program the dynamic menus. When I say this, I
don't mean actually loading the menu items dynamically. I suspect
that'll be the easy part. I mean opening a form, report or calling a
routine if the name of that form, report or routine is stored in a
variable.

i.e.

--Set Connection properties
--Open datareader--Load Menu and store the appropriate form, report or routine name to
open in the Tag property of the menu item.
--On click of the menu, open the form or report, or call the routine,
that's named in the Tag property.

Depending on how to do this, I don't necessarily agree with you that
the database approach is extremely complicated. For instance, in
Microsoft Access I would simply do a Docmd.OpenForm (menuitem.Tag) or a
Docmd.OpenRepo rt (menuitem.Tag) or an Eval(menuitem.T ag). If I
hardcode the entire menu and then remove items that aren't needed for
particular users, every time I add new functionality I'll always have
to remember to update the menu instead of simply adding another record
to a table. Also, if I'm delivering a new SQL Server report then my
code may not change at all - I just have to add another record to my
table and deliver the report to my clients. If I hardcode the entire
menu and then remove menu items that aren't needed, I'll have to update
the menu every time I add a report and that means I'll have to
re-deploy the application.

Perhaps this is the way I should have asked these questions:

How can I open a form if the name of the form is stored in a variable?
How can I call a routine if the name of the routine is stored in
variable?

I won't ask about opening a SQL Server report...If I can get those two
questions I can figure out this one easily enough.

Thanks again,

Steve

I see. That definitely changes the scope of the situation. From the
sound of this, you are building a client-server, rather than a web,
program.
As far as the SqlServer reports, if it is a report provided by
SqlServer, then they can be added as a record. If you are using Crystal
Reports within the VB.net environment, then that can also be added to
the server, and a report file can actually be stored in the database as
a BLOB.

But to look at what you can do dynamically with MS Access and try to
find the same thing with VB.net, you will frustrate yourself. Access is
a tigthly bound environment, whereas VB and SqlServer are not. Visual
FoxPro will allow macro substitution of function and form names, but I
haven't seen anything like that in VB. You might want to consider that
tool for this particular project.

Based on what you've described though, the approach I would take with
VB.net is to include as a function in a dll, the code for building the
menus. In the same dll, I would include the dynamic forms. Because the
menu is built dynamically, the action to take based on a menu selection
can be handled quite easily. A non-changing function call with the form
name/function name as a parameter will tell the dll what to do. That
way the interface won't change, which will be good when I update the dll
with another form or function. Providing the user with an updated dll
file will add the new functionality, provided of course that user has
the appropriate privileges.

In VB.net, a dll is the only way I can find to "dynamicall y" provide
functionality. When the app is compiled, it needs to know everything.

I hope this helps. It sounds like a fun project.

Tom
Apr 14 '06 #4

Thanks again Tom,

I've been reviewing some other posts and I think I'm almost where I
want to be. I've got this:

When I load my main menu, I'm adding the form name to open into the tag
of the ToolStripMenuIt em. I'm also adding an event handler to the
click of each menu item so that one "master" routine is called
regardless of which menu item is clicked. Inside my master routine I
check to see if the tag is filled with a form name. If it is, I call
this routine:

Public Sub OpenForm(prmFor mName as string)
If prmFormName = vbNullString Then
Exit Sub
End If

Dim nextForm As New Form
Dim nextFormReferen ce As String = String.Concat(M yAssembly & ".",
prmFormName)
Dim nextFormType As Type = Type.GetType(ne xtFormReference )

'nextForm.MdiPa rent = Me
nextForm = System.Activato r.CreateInstanc e(nextFormType)
nextForm.Show()
End Sub

One little glitch that I didn't mention before. My app is an mdi
application, so all of the these forms I'd like opened need to be
mdichildren of my main form. As you can see from the above, the line
where I set the form to be a child is commented out. With this line
commented out as it is, the correct form opens fine, but obviously it's
not an mdichild. However, the problem is that as soon as I uncomment
that line out, my form doesn't appear at all. I don't get any errors,
but my form doesn't show up. I don't know if it's being opened and
then immediately closed or if it's not opening at all.

I also found an alternative routine to do the same thing (please ignore
the naming conventions - I just copied from the Internet and made
slight modifications for now):

Private Sub OpenForm(ByVal prmFormName As String)
Dim app As System.Reflecti on.Assembly =
System.Reflecti on.Assembly.Get ExecutingAssemb ly()
Dim dlg As New Form
dlg = app.CreateInsta nce("ECCI_THA." & prmFormName)
'dlg.MdiParent = Me
dlg.Show()
End Sub

The only real difference between the two is that the first one uses
System.Activato r.CreateInstanc e and the second uses
System.Reflecti on.Assembly.Get ExecutingAssemb ly.CreateInstan ce.
I don't know what the difference is between these two or which is the
preferred method, but this second routine acts exactly as the first one
does - it'll show my form if I don't specify "mdiparent = me", but as
soon as I uncomment out that line my form does not show up.

Incidentally, this routine is of course behind my main form, so "Me" is
valid (and I tested it out by hardcoding a form name and opening it up
as an mdi child - everything worked fine).

Any help would be greatly appreciated. I think I'm almost there.

Thanks again.

Steve

Apr 14 '06 #5

Well, I've confirmed that when I use

nextForm.MdiPar ent = Me (in the first routine)

or

dlg.MdiParent = Me (in the second routine)

the form is not being opened at all (as opposed to being opened and
immediately closed).

Apr 14 '06 #6

Okay, I've done some more testing and I'm really stumped. I load my
menu as I've said before, and for each ToolStripMenuIt em, I add an
event handler so that when a menu item is clicked, it calls my routine:

I won't bore you with the actual code to load the menu - it loads fine
and when I click any menu item my routine gets called just fine.

For testing purposes, I added the following routine and a button on my
form.

Private Sub LoadForm
Dim frm As New frmTest
With frmTest
.MdiParent = Me
.Left = 0
.Top = 0
.Show()
End With

On the OnClick of my button, I call the above routine. The form loads
fine.
In my routine that's called from my menu, I call the above routine (and
nothing else). The form does not load.

I've confirmed that the routine is getting called regardless of whether
I'm hitting the button or selecting a menu item. So, I'm not sure why
my child form isn't loading via the menu clicks but is loading via the
button click.

Based on this it doesn't seem that my problem has anything to do with
the CreateInstance method I called before.

Steve

Apr 14 '06 #7
St*********@Eag leCCI.com wrote:
Okay, I've done some more testing and I'm really stumped. I load my
menu as I've said before, and for each ToolStripMenuIt em, I add an
event handler so that when a menu item is clicked, it calls my routine:

I won't bore you with the actual code to load the menu - it loads fine
and when I click any menu item my routine gets called just fine.

For testing purposes, I added the following routine and a button on my
form.

Private Sub LoadForm
Dim frm As New frmTest
With frmTest
.MdiParent = Me
.Left = 0
.Top = 0
.Show()
End With

On the OnClick of my button, I call the above routine. The form loads
fine.
In my routine that's called from my menu, I call the above routine (and
nothing else). The form does not load.

I've confirmed that the routine is getting called regardless of whether
I'm hitting the button or selecting a menu item. So, I'm not sure why
my child form isn't loading via the menu clicks but is loading via the
button click.

Based on this it doesn't seem that my problem has anything to do with
the CreateInstance method I called before.

Steve

I use a similar structure to open a couple of forms, and it works fine.
But I have one suggestion, only because I seem to recall someone else
bringing this up a while ago: don't use a with block. Call the methods
with frmTest. before all of them. Just a worthwhile test - I promise
nothing!

Tom
Apr 16 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

39
6552
by: Randell D. | last post by:
Folks, I'm sure this can be done legally, and not thru tricks of the trade - I hope someone can help. I'm writing a 'tool' (a function) which can be used generically in any of my projects. When it completes, it can call a success, or a failure function. The names of these success, or failure functions will differ, and I'd like to know how I can pass the name of a function to my tool, and how my tool can call the function, using that...
55
4675
by: Steve Jorgensen | last post by:
In a recent thread, RKC (correctly, I believe), took issue with my use of multiple parameters in a Property Let procedure to pass dimensional arguments on the basis that, although it works, it's not obvious how the code works if you don't know the intricacies of the Property Let/Get syntax. Likewise, I dislike (and code to minimize the use of) the VB/VBA syntax of returning a value by referring to the function name as if it were a...
4
3292
by: Koen | last post by:
Hi all, At work I created a database which is really helpful. The database is used by approx 15 users. Everything worked great, until I added some 'scoreboard' forms and reports. I get the "Cannot open any more databases" error. The 'scoreboard' form show a matrix of 6 columns, 7 rows. Each cell is calculated separate by (what I call complex) queries.
6
3649
by: Kenneth Courville | last post by:
Hello, I'm looking for assistance with the Access object model. I know this is VB, but I'm building an Office Add-using C# directed at Access 2002. I'm literate in VB, so you can reply in VB... I think my problem mainly lies in knowing the quirks of the Access object model. Basically, I'm looking for a method of determining if an Access database is open yet. AND I'm looking for a method that doesn't not require checking for an...
8
10770
by: lauren quantrell | last post by:
When I open an Access form I can have no recordset specified, then in the form's OnOpen event I can do something like: Me.paramaters = "@SomeColumn = 22)" Me.recordsource = "dbo.sproc123" But I can't do this in a report as it will prompt me for the parameters, even though they seem to be defined ahead of the recordsource. I have worked around this by opening reports to a bogus recordsource
3
7457
by: kmacon | last post by:
I have a form that contains a command button. The command button's OnClick event builds a report using the CreateReport and CreateReportControl functions and then opens the main report. I set the OnFormat property of the detail section in the subreport to a function which is located in a standard module. I am not able to access the subreport nor it's controls in the module. I need to hide a text box in the subreport if there is no...
6
18816
by: ultraton | last post by:
While trying to print a report from Access the user receives the following error: Cannot open any more databases. Okay Help Does anyone have any ideas about this behavior? Thank you very much.
4
1852
by: Ahmet | last post by:
Hi all; I have one application in which I read form names from database to be opened. I open form with the code below, and call its show method for form to be shown but before this, I must set MdiChild property to true to make this form child I can call functions as you can see like (show), how can i set.MdiParent = this property of dynamically loaded form ? Or I can call a form function dynamically with parameter ?...
13
3550
by: robert.waters | last post by:
Do you have your users rely on the windows taskbar to manage/navigate between the forms they have open, or do you provide another (more robust) method? Does anyone use a custom navigation system (like keeping a selectable list of open forms in a listbox/commandbar/ breadcrumb trail etc.)?
0
9583
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9990
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7406
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6668
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5297
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3955
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3560
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2814
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.