By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,561 Members | 3,010 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,561 IT Pros & Developers. It's quick & easy.

Convert string to a Form object

P: 82
Hi,
I found some material about this topic but it all refered to vb6.
I have a table which contains names of the forms in my system. After getting the form name out of the table, i want to set the "AllowEdits" property of that form to false, but for that i need to convert the string which contains the form name into a Form object, at least so i think.. here is a piece of my code:
Expand|Select|Wrap|Line Numbers
  1. Do While Not rs.EOF
  2.     Select Case rs("formNum")
  3.     Case 1:
  4.     rsForms.FindFirst ("formNum = ") & rs("formNum")
  5.     If rsForms.NoMatch = False Then
  6.         strFormName = rsForms("formEnglishName")
  7.         strFormName.AllowEdits = False
  8.         ....
  9.  
strFormName holds the form name, but what should i do in order for the last line (in bold) to work?
Thanks in advance
May 10 '10 #1
Share this Question
Share on Google+
10 Replies


Megalog
Expert 100+
P: 378
Are you delcaring strFormName as a form type, not a string?

Like:
Expand|Select|Wrap|Line Numbers
  1. Dim strFormName As Form
May 10 '10 #2

P: 82
@Megalog
Hi Megalog,
I actually declared it as string. That way i can assign it the form name, but can't assign form property (such as AllowEdits). And if i declare it as Form, than i can assign the property but don't have the name..
May 10 '10 #3

NeoPa
Expert Mod 15k+
P: 31,494
There are collections available that allow you to pass a string parameter to select the matching item.
Expand|Select|Wrap|Line Numbers
  1. {Application.}Forms     All currently open forms.
  2. CurrentProject.AllForms All forms in the project.
Forms("YourFormName") returns that form as long as it's already open and available. The {Application.} part is optional.
May 10 '10 #4

Megalog
Expert 100+
P: 378
This can go in after line 5 from your initial posting:

Expand|Select|Wrap|Line Numbers
  1. DoCmd.OpenForm rsForms!formEnglishName, acDesign
  2. If Forms(rsForms!formEnglishName).AllowEdits = True Then
  3.     Forms(rsForms!formEnglishName).AllowEdits = False
  4. End If
  5. DoCmd.Close acForm, rsForms!formEnglishName, acSaveYes
May 10 '10 #5

TheSmileyCoder
Expert Mod 100+
P: 2,321
Just out of curiosity what are you using this for?
May 10 '10 #6

P: 82
Wow, it works! Thank you Megalog!:) I just added acHidden to window mode, so it won't pop.
TheSmileyOne, i'm trying to implement restrictions for users. I created a form with a listbox filled with form names, so that whoever sets the configurations will choose set of forms for each worker, which he will not be allowed to modify. It's all my creation, so i'm sure it's not the most clever way, and i'm sure it will be modified not once, but at least what i have now works, thanks again.
Now i'm thinking about a way to add an option to choose exactly what restriciton will be implemented for each form, something like checkboxes for edits/additions/deletions.
May 11 '10 #7

P: 82
Is there a way to remember listbox selection?
The process is: I choose some values (form names) in the listbox, press button "save choice", then my code activates and the values go in the table as supposed to, but the next time i enter the form the listbox is fresh, and i want it to be exactly as it was when i left it (the chosen lines must be black). I thought maybe on form Load event go through the table which holds the chosen values, and make them appear as chosen, but how?
May 11 '10 #8

TheSmileyCoder
Expert Mod 100+
P: 2,321
This is some code I wrote myself, to convert the stored values into a comma delimite list, and store it in a hidden textbox, bound to a hidden TEXT field. (of course this only works if the length of the string wont exceed 255)

I only use this in a search form, to remember the users last performed search, so its not critical. If its something you need to store what user can access, I might want to write the data to tables instead of a string.

Anyways, here it is, and im sure you can modify it to read/restore the data of the table.
To STORE the values
Expand|Select|Wrap|Line Numbers
  1. Private Sub list_Status_BeforeUpdate(Cancel As Integer)
  2.  
  3.     StoreList Me.list_Status, Me.tb_status
  4.  
  5. End Sub
To RESTORE the values
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Current()
  2. 'Restore the list selections
  3.     restoreList Me.list_Status, Me.tb_status
  4.     restoreList Me.list_Grade, Me.tb_Severity
  5.  
  6. End Sub

The procedures used:
Expand|Select|Wrap|Line Numbers
  1. Public Sub StoreList(ctrlListBox As Control, ctrlTextBox As Control)
  2. 'Lets store the items selected in the listbox
  3.     'Check that ctrlListBox is truly a listbox
  4.     If ctrlListBox.ControlType <> acListBox Then
  5.         MsgBox "This is not a listbox!"
  6.         Exit Sub
  7.     End If
  8.  
  9.     'Check that ctrlTextbox is truly a textbox
  10.     If ctrlTextBox.ControlType <> acTextBox Then
  11.         MsgBox "This is not a textbox!"
  12.         Exit Sub
  13.     End If
  14.  
  15.     'Dim variables
  16.         Dim lvItem As Variant
  17.         Dim intI As Integer
  18.         Dim strStorage As String
  19.  
  20.     'Loop through selected items
  21.         For Each lvItem In ctrlListBox.ItemsSelected
  22.             strStorage = strStorage & "," & ctrlListBox.ItemData(lvItem)
  23.         Next
  24.  
  25.     'If no items are selected then
  26.         If strStorage & "" <> "" Then
  27.             ctrlTextBox = Right(strStorage, Len(strStorage) - 1)
  28.             Else
  29.             ctrlTextBox = Null
  30.         End If
  31.  
  32.  
  33.     Set lvItem = Nothing
  34.  
  35. End Sub
Expand|Select|Wrap|Line Numbers
  1. Public Sub restoreList(ctrlListBox As Control, ctrlTextBox As Control)
  2.     'Load the listbox
  3.     On Error GoTo exitSub
  4.  
  5.     'Init variables
  6.         Dim intI As Integer
  7.         Dim intCount As Integer
  8.         Dim myVar As Variant
  9.     'Unmark all
  10.         DoCmd.Echo (False) 'Turn of painting
  11.         For intI = 0 To ctrlListBox.ListCount
  12.             ctrlListBox.Selected(intI) = False
  13.         Next
  14.  
  15.     'If nothing is selected, go ahead and end sub
  16.         If ctrlTextBox & "" = "" Then GoTo endSub
  17.  
  18.     'How many items are marked as selected
  19.         intCount = CharCount(ctrlTextBox, ",")
  20.  
  21.     Dim intJ As Integer
  22.     'Restore teh selections
  23.         For intI = 0 To intCount
  24.             myVar = CVar(Split(ctrlTextBox, ",")(intI))
  25.             For intJ = 0 To ctrlListBox.ListCount
  26.                 If ctrlListBox.ItemData(intJ) = myVar Then
  27.                     ctrlListBox.Selected(intJ) = True
  28.                 End If
  29.  
  30.             Next
  31.         Next
  32.  
  33. endSub:
  34.     DoCmd.Echo (True)
  35.  
  36. Exit Sub
  37. exitSub:
  38. Debug.Print Err.Number & " - " & Err.Description
  39. DoCmd.Echo (True)
  40.  
  41. End Sub
May 11 '10 #9

Megalog
Expert 100+
P: 378
If you're using A2007, you can create a multivalue field in the table, and bind the listbox to it. Then it'll remember all the selections you make. Quite quick to set up, but the downside is that using MV fields is a real pain if you need to extract values out of there for use elsewhere.
May 11 '10 #10

P: 82
TheSmileyOne, thanks a lot for the code. My values are stored it a table anyway, but i did use the ".Selected" property, it does paint the line as chosen, for some reason i didn't find it earlier ... now i have another problem, but i'll try to figure it out.
Megalog, i'm using Access 2003
May 12 '10 #11

Post your reply

Sign in to post your reply or Sign up for a free account.