General VB program structure

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 1595 Views - Last Post: 30 August 2013 - 07:56 PM Rate Topic: -----

#1 Roland_J  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 18-August 13

General VB program structure

Posted 18 August 2013 - 11:03 AM

Hi All

My first post here.
I am used to programming in assembler, and battling to understand, on my first VB program, how the program structure operates.

My quandary is that everything is in modules, or subs, so how do they inter-relate. Do you 'call' everything?
For example; Lets say a button is pressed, that makes a Label becomes visible. So I have a Private Sub Command_Click() that does this. Then I decide that I want the Label that 'lit' up to disappear after 2 seconds.

Now if I was writing assembler, I'd simply insert a "for n = 1 to" delay in the routine. But in VB I have to set up a whole new sub? Is that right? Must I now call this sub?

If I look at the tutorials on timers, they're all very conveniently? soley timer routines, and seem to do nothing else.
Also, lets say at the end of the 2 seconds in the example above, I wanted to see if there was a character coming in the serial port. In assembler I just have to check one bit of a Uart register, but in VB I seem to need another sub with about ten lines of code.

This is not a complaint, I'm just trying to figure out how the program structure works. Where do I put my 'Main' program? If someone could point me to a good discourse, it would be great. Tutorials are no good because they look at each function in isolation, and not how all the components interact.
Help appreciated.

Regards
Roland

Is This A Good Question/Topic? 0
  • +

Replies To: General VB program structure

#2 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2875
  • View blog
  • Posts: 9,540
  • Joined: 12-December 12

Re: General VB program structure

Posted 18 August 2013 - 11:18 AM

Are you using VB6 (why?) or VB.NET, also known as Visual Basic 2010 (or other year)?

Ahead of your response, though, your statement "tutorials are no good" is, erm, puzzling. How are you going to cover the basics, the fundamentals, without following some kind of tutorial?
Was This Post Helpful? 0
  • +
  • -

#3 Roland_J  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 18-August 13

Re: General VB program structure

Posted 18 August 2013 - 11:40 AM

Hi Andrew

I am using VB6 because it's now free, and I think sufficient for my needs. I'm running on XP. Also the other programs that I want to use will accept VB scripts.

Ok, 'tutorials are no good' is the reaction to my slowness in learning. I have been reading tutorials for three days, but I still don't see program flow. A tutorial would show a delay using the Timer, as a stand alone sub. As a more direct question, if I have the code below, how would I insert a 2 second delay at the shown insert points?

Private Sub Form_Vis1()
If a = 1 Then
 Label1.Visible = True
                             << how do I insert 2 second delay here
  Label2.Visible = False
   Else
  Label1.Visible = False
 Label2.Visible = True
End If
End Sub


Help appreciated.
Regards
Roland

This post has been edited by andrewsw: 18 August 2013 - 11:46 AM
Reason for edit:: Please use CODE tags

Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2875
  • View blog
  • Posts: 9,540
  • Joined: 12-December 12

Re: General VB program structure

Posted 18 August 2013 - 11:57 AM

You can cause a delay by including a Win-API call:

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

While IsStillWaitingForSomething()
    DoEvents
    DoEvents
    Sleep(55)
Wend

(I'm not sure why a delay should be an early concern?!)

Visual Studio Express is completely free, and up-to date (Is VB6 free?) and you wouldn't be starting with technology that is out-dated and unsupported.

Unfortunately, I am not currently inspired to describe how VB6 applications are/can be structured, and would only refer you to tutorials. Good luck.

This post has been edited by andrewsw: 18 August 2013 - 11:58 AM

Was This Post Helpful? 1
  • +
  • -

#5 Roland_J  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 18-August 13

Re: General VB program structure

Posted 18 August 2013 - 12:15 PM

Thanks Andrew

This is what confuses me. Can I simply insert the delay inside my code, like this? Structure wise, that is.

Private Sub Form_Vis1()
	If a = 1 Then
	 Label1.Visible = True
	   
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
	While IsStillWaitingForSomething()
	    DoEvents
	    DoEvents
	    Sleep(55)
	Wend

	  Label2.Visible = False
	   Else
	  Label1.Visible = False
	 Label2.Visible = True
	End If
	End Sub




Regards
Roland
Was This Post Helpful? 0
  • +
  • -

#6 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2875
  • View blog
  • Posts: 9,540
  • Joined: 12-December 12

Re: General VB program structure

Posted 18 August 2013 - 12:24 PM

Quote

Where do I put my 'Main' program?

Well, to clarify this point briefly, if you create a Console Application (I don't recall if this shows as Console Application or Standard Exe in the New Project dialog) then this has a Sub Main() which runs when the application is started, and the application exits when it reaches the end of this procedure.

If you create a (Win)Form Application then there is, effectively, a hidden Main but all it does is to load the first form (or the one that you have designated as the main form for your application). When this form is closed the application exits. VB6 is event-driven so all other code is executed on clicking buttons, or other control-events.

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

You can't put this declaration in the middle of a procedure, it needs to be at the top of a Module. Thinking that you could do this just indicates that you should take a full tutorial.

This post has been edited by andrewsw: 18 August 2013 - 12:27 PM

Was This Post Helpful? 1
  • +
  • -

#7 Roland_J  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 18-August 13

Re: General VB program structure

Posted 18 August 2013 - 12:51 PM

Thanks Andrew

I do appreciate that declarations need to be at the top, but it still does not explain how one would normally insert a delay at any one point in a program, unless I did this?

Private Sub Form_Vis1()
	If a = 1 Then
	 Label1.Visible = True
	
       Call Sleep

	  Label2.Visible = False
	   Else
	  Label1.Visible = False
	 Label2.Visible = True
	End If
	End Sub



Regards
Roland
Was This Post Helpful? 0
  • +
  • -

#8 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2875
  • View blog
  • Posts: 9,540
  • Joined: 12-December 12

Re: General VB program structure

Posted 18 August 2013 - 02:47 PM

Well it would be
Call Sleep(200)    'ms

as you need to supply the interval.

You seem, erm, pre-occupied with a delay. An artificial delay goes against the nature or intent (the paradigm) of an event-driven system. If you want a delay just stretch your arms before clicking a button :bananaman:

A form-based application has delays in-built into the system. It is constantly waiting, listening for events (messages) to respond to.

This post has been edited by andrewsw: 18 August 2013 - 02:51 PM

Was This Post Helpful? 0
  • +
  • -

#9 Roland_J  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 18-August 13

Re: General VB program structure

Posted 18 August 2013 - 09:22 PM

Thanks Andrew

The reason for my questions is that I want to use VB6 to control a small machine. The user will make certain choices on the screen, but then I want to use the program to control inputs, motors and relays, so in that regard I want to use VB6 as a 'normal' program, and not just event driven.

Regards
Roland
Was This Post Helpful? 0
  • +
  • -

#10 maj3091  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 299
  • View blog
  • Posts: 1,755
  • Joined: 26-March 09

Re: General VB program structure

Posted 19 August 2013 - 06:38 AM

Hi Roland,

The points Andrew has made in regards to inserting an inline delay are completely valid.

I'm struggling understanding why that wouldn't fit in with what you're trying to do?

You haven't specified what you're using for your interface hardware, but does this not come with control libraries and/or example code?
Was This Post Helpful? 0
  • +
  • -

#11 Roland_J  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 18-August 13

Re: General VB program structure

Posted 20 August 2013 - 04:59 AM

It does sound like I've started off on the wrong foot on this Forum. The focus on the delay is also a bit of a red herring. I am more concerned with how the different program parts interact.

The main problem, I think, is that I have been trying to find VB6 examples of using the call function, and not realising that the word 'call' is not used. Obviously optional though.

My interface hardware will be made up. I'm using a pic controller board to control small DC motors. The pic will talk to the PC (VB6 program) using a small command set over USB.

Regards
Roland
Was This Post Helpful? 0
  • +
  • -

#12 maj3091  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 299
  • View blog
  • Posts: 1,755
  • Joined: 26-March 09

Re: General VB program structure

Posted 20 August 2013 - 06:23 AM

OK, so the majority of the work will be done in the pic micro then and VB will just give a front end to the "machine"?

I used to work in automation and have used VB6 as a front end in the past, so I'm just trying to get a better grasp of what you're trying to achieve and which parts of the system lie where in regards to what does what.

Taking the comms out of the equation (as that's a different ball game altogether), can you give a specific example of something you want to do and that you're struggling understanding, that way, I or someone else can give you a more specific answer.
Was This Post Helpful? 0
  • +
  • -

#13 Roland_J  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 18-August 13

Re: General VB program structure

Posted 21 August 2013 - 10:04 AM

Hi

The questions will come as I get deeper into the whole thing. I'm still very busy with the mechanics at the moment.

The machine is a test station to inspect an automotive part. A camera, using 'RoboRealm' will inspect the part and check criteria. Motors move the part and the panel. These will be controlled by a Pic using simple commands via USB to RS232. The pic will always be polled only, to keep it simpler.

The part to be tested has 24 variants, with 12 criteria, and that's why I wanted to use a monitor, so I could easily change the displayed test codes instead of re-labelling buttons. But to make the system hardier, I have used ATM style buttons at the side of the screen. So now to recognise a key-press, I'm using another pic board as a keyboard scanner.

The PC is required to run Roborealm, archive the images, and be able to run diagnostics. While I could use a generic I/O card on the PC, being able to ramp the motor current up and down, as well as the other I/O required will mean more than one board, and I have the bulk of this done through other projects.
Was This Post Helpful? 0
  • +
  • -

#14 maj3091  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 299
  • View blog
  • Posts: 1,755
  • Joined: 26-March 09

Re: General VB program structure

Posted 21 August 2013 - 10:29 AM

Thanks for the detailed explanation on how this will hang together, it's useful to know.

So, back to basics then....

What part, or what do you require your VB program to do in all this? I noticed RoboRealm as a published API, are you planning on using that to communicate with RoboRealm from your VB program?

Sorry for all the questions (probably seems like I'm requesting more than offering at this stage).

This post has been edited by maj3091: 21 August 2013 - 10:31 AM

Was This Post Helpful? 0
  • +
  • -

#15 Roland_J  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 18-August 13

Re: General VB program structure

Posted 21 August 2013 - 11:00 AM

The VB program is going to display 6 variant codes on the screen. The supervisor will select a variant, or press a key to display the next 6, until a choice is made. Lets say it's choice 3 of 24. A single variable, 3, will be passed to Roborealm. The VB program and Roborealm are on the same PC.

When the operator loads a part and presses start, Roborealm will sequence through the images and set/clear bits using template 3 to arrive at a pass/fail result. This variable will be available to the VB program to show a pass/fail on the screen.

There are a number of things that need to happen, but I think it is actually quite simple. If I could use a Pic alone, there would be no problem. I haven't mentioned that a Fanuc robot actually moves the camera, but that interface will be no more than four I/O lines to control, like 'start' 'stop' 'move to next' and 'in position' feedback.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2