473,320 Members | 2,024 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 10027
St*********@EagleCCI.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.OpenReport (menuitem.Tag) or an Eval(menuitem.Tag). 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*********@EagleCCI.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.OpenReport (menuitem.Tag) or an Eval(menuitem.Tag). 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 "dynamically" 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 ToolStripMenuItem. 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(prmFormName as string)
If prmFormName = vbNullString Then
Exit Sub
End If

Dim nextForm As New Form
Dim nextFormReference As String = String.Concat(MyAssembly & ".",
prmFormName)
Dim nextFormType As Type = Type.GetType(nextFormReference)

'nextForm.MdiParent = Me
nextForm = System.Activator.CreateInstance(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.Reflection.Assembly =
System.Reflection.Assembly.GetExecutingAssembly()
Dim dlg As New Form
dlg = app.CreateInstance("ECCI_THA." & prmFormName)
'dlg.MdiParent = Me
dlg.Show()
End Sub

The only real difference between the two is that the first one uses
System.Activator.CreateInstance and the second uses
System.Reflection.Assembly.GetExecutingAssembly.Cr eateInstance.
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.MdiParent = 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 ToolStripMenuItem, 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*********@EagleCCI.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 ToolStripMenuItem, 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
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. ...
55
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...
4
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...
6
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...
8
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...
3
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...
6
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...
4
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...
13
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...
0
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...
0
isladogs
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...
1
isladogs
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...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
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...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
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...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
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 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.