VBA
LINKS BELOW IN BUTTONS
LINKS BELOW IN BUTTONS
Article
09/13/2021
2 minutes to read
Is this page helpful?
Keyword
Used in these contexts
As
Binary
ByRef
ByVal
Date
Else
Empty
Used as a Variant subtype that indicates an uninitialized variable value.
Error
False
Has a value equal to 0.
For
Friend
See Friend.
Get
Input
Is
Len
Let
Lock
Me
See Me.
Mid
New
Next
Nothing
See Nothing.
Null
Used as a Variant subtype that indicates that a variable contains no valid data.
On
Option
Optional
ParamArray
Private
Property
PtrSafe
See PtrSafe.
Public
Resume
Seek
Set
Static
Step
String
Then
Time
To
True
Has a value equal to -1.
WithEvents
Visual Basic Example Code
Below is an example VB program that does the same thing as the "Demo" program that ships with Netica C-API. There is a Visual Studio project for it, called "Netica Demo for VB" within the " Netica\Netica xxx\Programming Examples " folder of the Netica download package. (more info on programming Netica in VB)
Sub Main()
On Error GoTo Failed
Dim app As Netica.Application
app = New Netica.Application
app.Visible = True
Dim net_file_name As String
net_file_name = System.AppDomain.CurrentDomain.BaseDirectory() & "..\..\..\ChestClinic.dne"
Dim net As Netica.Bnet
net = app.ReadBNet(app.NewStream(net_file_name))
net.Compile()
Dim TB As Netica.BNode
TB = net.Nodes.Item("Tuberculosis")
Dim belief As Double
belief = TB.GetBelief("present")
MsgBox("The probability of tuberculosis is " & belief)
net.Nodes.Item("XRay").EnterFinding("abnormal")
belief = TB.GetBelief("present")
MsgBox("Given an abnormal X-Ray, the probability of tuberculosis is " & belief)
net.Nodes.Item("VisitAsia").EnterFinding("visit")
belief = TB.GetBelief("present")
MsgBox("Given abnormal X-Ray and visit to Asia, the probability of tuberculosis is " & belief)
net.Nodes.Item("Cancer").EnterFinding("present")
belief = TB.GetBelief("present")
MsgBox("Given abnormal X-Ray, Asia visit, and lung cancer, the probability of tuberculosis is " & belief)
net.Delete()
If Not app.UserControl Then
app.Quit()
End If
Exit Sub
Failed:
MsgBox("NeticaDemo: Error " & (Err.Number And &H7FFFS) & ": " & Err.Description)
End Sub
Below is an example that reads in a net from the Examples folder (you may have to change the path), then reads in cases and does belief updating.
------------------------------
Sub Main()
On Error GoTo Failed
Dim app As Netica.Application
app = New Netica.Application
Dim casefile As Streamer
Dim net As Bnet
Set netfile = app.NewStream("C:\Netica Data\BNs\Car_Diagnosis_0_Learned.dne")
Set casefile = app.NewStream("C:\Netica Data\Cases\Good Cases\Car Cases 10.cas")
Set net = app.ReadBNet(netfile)
net.AutoUpdate = 1
net.Compile
Dim lights_node As Bnode
Set lights_node = net.Node("Lights")
Dim lights_dim As Long
lights_dim = lights_node.GetStateIndex("dim")
Dim id As Long
Dim fr As Double
Dim caseposn As Long
Dim done As Boolean
done = False
caseposn = FirstCase
Do
net.RetractFindings
net.ReadFindings case_posn:=caseposn, stream:=casefile, IDNum:=id, freq:=fr
net.ReadFindings case_posn:=caseposn, stream:=casefile, nodes:=net.Nodes, IDNum:=id, freq:=fr
If caseposn = NoMoreCases Then
done = True
Else
MsgBox "Belief in Lights dim = " & lights_node.GetBelief(lights_dim)
End If
caseposn = NextCase
Loop Until done
net.Delete
Exit Sub
Failed:
MsgBox "Error " & ((err.Number And &H7FFF) - 10000) & ": " & err.Description
End Sub
===============================================================================
EXAMPLES ON HOW TO SET CPT TABLE ENTRIES:
-----------------------------------------
Here is how you could set the CPTs of the "Chest Clinic" example from the manual:
Dim VisitAsia As BNode, Tuberculosis As BNode, Smoking As BNode
Dim Cancer As BNode, XRay As BNode, TbOrCa As BNode
Set VisitAsia = net.Node("VisitAsia")
...
Set TbOrCa = net.Node("TbOrCa")
Dim p(0 To 1) As Single
p(0) = 0.01: p(1) = 0.99: VisitAsia.CPTable("") = p
p(0) = 0.05: p(1) = 0.95: Tuberculosis.CPTable(Array(0)) = p
p(0) = 0.01: p(1) = 0.99: Tuberculosis.CPTable(Array(1)) = p
p(0) = 0.5: p(1) = 0.5: Smoking.CPTable("") = p
p(0) = 0.1: p(1) = 0.9: Cancer.CPTable(Array(0)) = p:
p(0) = 0.01: p(1) = 0.99: Cancer.CPTable(Array(1)) = p
p(0) = 0.98: p(1) = 0.02: XRay.CPTable(Array(0)) = p
p(0) = 0.05: p(1) = 0.95: XRay.CPTable(Array(1)) = p
p(0) = 1: p(1) = 0: TbOrCa.CPTable(Array(0, 0)) = p:
p(0) = 1: p(1) = 0: TbOrCa.CPTable(Array(0, 1)) = p:
p(0) = 1: p(1) = 0: TbOrCa.CPTable(Array(1, 0)) = p:
p(0) = 0: p(1) = 1: TbOrCa.CPTable(Array(1, 1)) = p:
Here are 6 alternate ways to set the CPT of the TbOrCa node.
Dim p(0 To 1) As Single
Dim s(0 To 1) As Integer
s(1) = 0: s(0) = 0: p(0) = 1: p(1) = 0: TbOrCa.CPTable(s) = p:
s(0) = 1: TbOrCa.CPTable(s) = p:
s(1) = 1: s(0) = 0: TbOrCa.CPTable(s) = p:
s(0) = 1: p(0) = 0: p(1) = 1: TbOrCa.CPTable(s) = p:
Dim p(0 To 1) As Single
p(0) = 1: p(1) = 0: TbOrCa.CPTable("present,present") = p:
p(0) = 1: p(1) = 0: TbOrCa.CPTable("present,absent") = p:
p(0) = 1: p(1) = 0: TbOrCa.CPTable("absent,present") = p:
p(0) = 0: p(1) = 1: TbOrCa.CPTable("absent,absent") = p:
TbOrCa.StateFuncTable("present,present") = "true":
TbOrCa.StateFuncTable("present,absent") = "true":
TbOrCa.StateFuncTable("absent,present") = "true":
TbOrCa.StateFuncTable("absent,absent") = "false":
TbOrCa.CPTable("*,*") = Array(1, 0)
TbOrCa.CPTable("absent,absent") = Array(0, 1)
TbOrCa.StateFuncTable("*,*") = "true"
TbOrCa.StateFuncTable("absent,absent") = "false"
TbOrCa.Equation = "TbOrCa (Tuberculosis, Cancer) = (Tuberculosis || Cancer)"
TbOrCa.EquationToTable num_samples:=1, samp_unc:=False, add_exist:=False
FORMULA : Simple Interest = Principle x Rate x Time / 100
CLS
INPUT ” Enter the Principal”;P
INPUT ” Enter the Rate”;R
INPUT ” Enter the Time”;T
LET I = P*T*R/100
PRINT ” The simple Interest = “;I
END
Example 10:
FORMULA : Simple Interest = Principle x Rate x Time / 100
Amount = Principle + Simple Interest
CLS
INPUT ” Enter the Principal”;P
INPUT ” Enter the Rate”;R
INPUT ” Enter the Time”;T
LET I = P*T*R/100
LET A= P + I
PRINT ” The simple Interest = “;I
PRINT ” The amount=”;A
END
So, these are some common Qbasic programming examples that are useful for students and new programmers. These posts will be updated frequently with new Qbasic examples and exercises, so keep visiting for more fun programming exercises.
CONFIDENTIALITY / COPYRIGHT NOTICE: This site and my information and any attachments contains the PRIVILEGED AND CONFIDENTIAL INFORMATION of Fred Finkelstein & Irondesigner DBA / LLC mutually, Inc., its affiliated corporations or legal entities, and is intended only for the use of the individual(s) named above. If you are not the intended recipient of this e-mail or invited to this site, or the employee or agent responsible for delivering this to the intended recipient, you are hereby notified that any unlawful interception, dissemination, disclosure, printing or copying of this e-mail or site or any attachments is strictly prohibited under the Electronics Communication Privacy Act (ECPA), 18 USCA 2510, 18 USCA 2511, and any applicable laws. If you have received this e-mail or viewing this site in error, please delete or destroy it & close down, including all attachments or copies, and immediately notify us by e-mail at mechanicalengrg@gmail.com