Author Topic: Function this!!  (Read 3119 times)

0 Members and 1 Guest are viewing this topic.

hardwired

  • Guest
Function this!!
« on: March 05, 2008, 06:28:37 AM »
Hi,

I have the following code and was wondering how i can make it into a function with calls to it from 10 different buttons - the only thing changing would be the the textbox reference where the block name goes (PICKED), but that will be put into each button's click sub. I just want to avoid having every button with all this code


Code: [Select]
Private Sub blockpick1BTN_Click()
FixingsChartFRM.Hide 'Hide the form..
On Error Resume Next
PICK:
ThisDrawing.Utility.GetEntity FixBlock, PickPoint, "Please select a fixing.."

If Err <> 0 Then
    Err.Clear
    Exit Sub
Else 'Else for error..
    If FixBlock.ObjectName = "AcDbBlockReference" Then 'If block..
        If FixBlock.Name Like "A$C*" Then 'If the block name is "A$C......." (A copied and pasted block)..
            MsgBox "This isn't a true fixing block - It appears to be a copied / pasted block.." & vbCr & "Please pick another instance of it", vbExclamation, "Fixings Chart Creator.."
            GoTo PICK
        Else
            'ENTER ATTRIBUTE CODE HERE GETTING "FIXDESC" STRING..
        End If
    ElseIf FixBlock.ObjectName <> "AcDbBlockReference" Then 'If NOT block..
        ThisDrawing.Utility.Prompt "You may only select a block: "
        GoTo PICK
    End If 'End If for objectname..
End If 'End if for error..

PICKED:
fx1descTXT.text = FixBlock.Name & ": " & FixBlock.Handle 'Put the block name in the text box..
FixingsChartFRM.Show 'Show the form..
End Sub


.....I've never done finctions before and when i tried setting one up for this, i tried calling it from each button by just simply putting the name of the function, but it flagged up an error..

Bryco

  • Water Moccasin
  • Posts: 1883
Re: Function this!!
« Reply #1 on: March 05, 2008, 09:59:58 AM »
Better to show us your function.

hardwired

  • Guest
Re: Function this!!
« Reply #2 on: March 05, 2008, 12:20:56 PM »
Hi,

As i said, i'm not hot on function as i've never created one before, just copied some form sources now and again, so please bear with me, and let me know if i've coded this incorrectly:


Code: [Select]
Public Function GETFIXINGBLOCK(FixBlock As AcadObject, PickPoint As Variant)
On Error Resume Next
PICK:
ThisDrawing.Utility.GetEntity FixBlock, PickPoint, "Please select a fixing.."

If Err <> 0 Then
    Err.Clear
    Exit Function
Else 'Else for error..
    If FixBlock.ObjectName = "AcDbBlockReference" Then 'If block..
        If FixBlock.Name Like "A$C*" Then 'If the block name is "A$C......." (A copied and pasted block)..
            MsgBox "This isn't a true fixing block - It appears to be a copied / pasted block.." & vbCr & "Please pick another instance of it", vbExclamation, "Fixings Chart Creator.."
            GoTo PICK
        Else
            ' Other code to go here later for getting the block attribute values but for now, just the block name..
            GoTo PICKED
        End If
    ElseIf FixBlock.ObjectName <> "AcDbBlockReference" Then 'If NOT block..
        ThisDrawing.Utility.Prompt "You may only select a block: "
        GoTo PICK
    End If 'End If for objectname..
End If 'End if for error..
End Sub


....The call from the button click looks like this:

Code: [Select]
Private Sub blockpick1BTN_Click()
    FixingsChartFRM.Hide 'Hide the form..
    GETFIXINGBLOCK
PICKED:
    fx1descTXT.text = FixBlock.Name & ": " & FixBlock.Handle 'Put the block name in the text box..
    FixingsChartFRM.Show 'Show the form..
End Sub



Joro--

  • Guest
Re: Function this!!
« Reply #3 on: March 05, 2008, 12:57:46 PM »
As far as I can see you don't need Function, you need a Sub with parameter where the parameter is the TextBox. I used your first code :
Code: [Select]
Sub ForButton(ByRef TB As TextBox)
FixingsChartFRM.Hide 'Hide the form..
On Error Resume Next
PICK:
Dim FixBlock As AcadEntity
Dim PickPoint As Variant
ThisDrawing.Utility.GetEntity FixBlock, PickPoint, "Please select a fixing.."

If Err <> 0 Then
    Err.Clear
    Exit Sub
Else 'Else for error..
    If FixBlock.ObjectName = "AcDbBlockReference" Then 'If block..
        If FixBlock.Name Like "A$C*" Then 'If the block name is "A$C......." (A copied and pasted block)..
            MsgBox "This isn't a true fixing block - It appears to be a copied / pasted block.." & vbCr & "Please pick another instance of it", vbExclamation, "Fixings Chart Creator.."
            GoTo PICK
        Else
            'ENTER ATTRIBUTE CODE HERE GETTING "FIXDESC" STRING..
        End If
    ElseIf FixBlock.ObjectName <> "AcDbBlockReference" Then 'If NOT block..
        ThisDrawing.Utility.Prompt "You may only select a block: "
        GoTo PICK
    End If 'End If for objectname..
End If 'End if for error..

PICKED:
TB.Text = FixBlock.Name & ": " & FixBlock.Handle 'Put the block name in the text box..
FixingsChartFRM.Show 'Show the form..
End Sub

You can put this code to each of the buttons changing just the TB parameter

Bryco

  • Water Moccasin
  • Posts: 1883
Re: Function this!!
« Reply #4 on: March 05, 2008, 07:30:38 PM »
Good point Joro, actually since textbox is part of the  form class you don't have to pass that either, it's on tap.

hardwired

  • Guest
Re: Function this!!
« Reply #5 on: March 06, 2008, 04:28:34 AM »
So, i will have to copy the code to each button click event, just changing the TB to TB1 or TB2 etc...?



Joro--

  • Guest
Re: Function this!!
« Reply #6 on: March 06, 2008, 06:57:12 AM »
No, you put this in one of your modules.

Then for the Click event of the first button you have:

Code: [Select]
Private Sub blockpick1BTN_Click()
     ForButton FixingsChartFRM.TextBox1
Exit Sub

For the second button (may be its name is blockpick2BTN) you have:

Code: [Select]
Private Sub blockpick2BTN_Click()
     ForButton FixingsChartFRM.TextBox2
Exit Sub

and so on...

hardwired

  • Guest
Re: Function this!!
« Reply #7 on: March 06, 2008, 07:27:48 AM »
Right, got you now, and it works like a dream - not like the one i had last night, that was hella bizarre, lol

Thanks Joro :)

Joro--

  • Guest
Re: Function this!!
« Reply #8 on: March 06, 2008, 08:50:55 AM »
I'm glad that's what you need!