Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calling sections of code

Status
Not open for further replies.

policechiefwiggum

Programmer
Mar 19, 2006
48
GB
Hi All,

Sorry i'm not 100% on the correct term's.

I have a part of code that that i've called 'ValidInput:'

this is part of a very long code, what i want to know is if i can call just this part of code from another form?

I've set the whole piece of code as a public sub, which i assume is required, and i assume i have to code something like
Code:
Call Form_FormName
but i'm not sure how to then focus on just the part of the code i want, i've tried
Code:
 Call Form_FormName, ValidInput
with no luck!!

Thanks as always
 
You call a procedure, not a portion of it.
You may either use parameters and some conditions in the code or use a more modular technique.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
From the looks of your code, you're trying to "call" a form, or as PHV pointed out, a part of the form...

Are you wanting to call the procedures or the form itself? You don't "call" a form, you open it.

Open a form:
Code:
DoCmd.Open Form, "MyFormName"

For a public procedure, you can do this:

In a Standard Module, not the Form's module (so, you can cut the code from the Form module, and paste it into a new Standard Module.

Then if you have the code as:
Code:
Private Sub DoSomeStuff()
  'Code here
End Sub

You could change that to:
Code:
Sub DoSomeStuff()
  'Code here
End Sub

or

Code:
Public Sub DoSomeStuff()
  'Code here
End Sub


Then in your Form module, when you want to run the particular code, all you have to type is:

Code:
Private Sub MyButton_Click()
  DoSomeStuff
End Sub
 
Oh, if you're wanting to open a form, and then specifically "go to" a particular control ON that form, you would need to use something like:
Code:
Private Sub SomeProcedure()
  [GREEN]'Other stuff done here...
  'If you want to close the current form when you open the next form..[/GREEN]
  DoCmd.Close 
[GREEN]  'To open the new form..[/GREEN]
  DoCmd.Open Form, "MyFormName"
[GREEN]  'To set the active focus to a specific control on the form..[/GREEN]
  Forms!MyFormName!SpecificControl.SetFocus
End Sub

Well, my context may be a little off... if it doesn't work, try tinkering with the ! vs . perhaps
 
Normally you would put a public sub in a standard module and call that sub from anywhere in the application.

So your original solution could be to simply move it off of the original form module and put it into a standard module. Now both the original form and your other form can call it.

However you can call a public procedure in a form's module as well. A form module is a class module so you have to call it by its class. Assume "formName" with procedure "validInput". To call it from outside the form

call Form_formName.validInput

You just had a comma instead of a period
 
ok, looks like i've not made myself clear, sorry!!

i have form a open, the user clicks "save" then i have code which says check fields 1,2,3,&4 have a value, if anyone of them is blank open form b.

if all 4 have a value run the save SQL


if form b is open it passes what (if any) values from form a to form b, form b then displays what is available, and gives the user the option to add the missing data in to form b, this is then updated in to form a (which is still open)if the data is not to hand the user then ticks a check box to acknowledge they need to source the missing info.

the user then clicks a "continue" button on form b, what i want this to do is then run the SQL on form a which will save the data from all fields and not just the 4 on form b then close form a, and then close form b.

following PHV's reply i've been and cut as much of the the SQL from form a and put it an a sub so i can call it from form a when required and was hoping to call from form b but this is giving me an error.

not sure if i need to do this but in the hope it might work i've changed all the 'Me.' to 'Form_a.' in my sub.
 
Hmm, is Form A not bound to your table?

If it is, then all you have to do is...

Code:
FormBContinueButton_Click()
'I'm just assuming you've got Field1, Field2, etc for the field names, which may not be correct.

  Dim ctl as Control
  Dim x As Integer

  For x = 1 To 5 'Or however many you have
    Set ctl = Form.Controls("Field" & x)
    If IsNull(ctl.Value) Then
    Else
      If IsNull(FormA.Controls("Field" & x).Value) Then
        Forms!FormA.Controls("Field" & x).Value = ctl.Value
      End If
    End If
  Next x

  Forms!FormA.Save
  Set ctl = Nothing
  x = 0
End Sub

**IMPORTANT - this code is totally untested, and I'm likely forgetting some syntax. Therefore, chances are, you'll get some errors if you use it just like this.. it's mainly just to get the idea across... and you'll still need whatever other code you were previously running for handling which form is open...
 
Nope! None of the fields are bound, the database is hosted on a network drive, I was advised by a friend who's pretty hot on these things (but has now migrated to Thailand, so I can't consult with) that binding the fields when hosted remotely will slow down performance.

If it's too difficult to do as I planned, I'll run the SQL on form a then update just the 4 fields on form b.

I was just trying to be clever!! (with only self taught SQL / VBA knowledge!)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top