Solved

Is it possible to run subs within Select Case statements?

Posted on 2006-10-20
11
199 Views
Last Modified: 2010-04-23
Experts,

I have a select case statement as follows;

        For Each currentNode In currentNodes
            Select Case currentNode.ToString
                Case "Hardware"
                    QueryHardware(SourceFileName)
                Case "Software"
                    QuerySoftware(SourceFileName)
                Case "SoftwareUnchecked"
                    QueryUnchecked(SourceFileName)
            End Select

I am getting QueryHardware(SourceFileName) not declared within the debugger, however I have created a private sub called QueryHardware elsewhere.

Is it not possible to run subs within case statements?
0
Comment
Question by:nickmarshall
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 2
  • +1
11 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 17772573
yes, that should be possible.

>however I have created a private sub called QueryHardware elsewhere.
well, I guess changing from private to protected, friend, protected friend or public, depending on the scope, this will solve the problem
0
 
LVL 1

Author Comment

by:nickmarshall
ID: 17772581
I have tried altering the sub to public, which I also thought would solve the problem however it remains the same.
0
 
LVL 21

Expert Comment

by:K V
ID: 17772592
obviously, u can use subs
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 17772594
well, where is the 'elsewhere' ?
is the function part of a class? if yes, you have to have an object of that class and use it's method ...
0
 
LVL 9

Expert Comment

by:DjDezmond
ID: 17772602
>>                Case "Hardware"
                      QueryHardware(SourceFileName)

If this QueryHardware sub is in the same class as your select statement, then make sure its set to private and use
Me.QueryHardware.

If its still not working, will you post the QueryHardware sub?
0
 
LVL 9

Expert Comment

by:DjDezmond
ID: 17772609
and its location... as AngelIII says...
0
 
LVL 1

Author Comment

by:nickmarshall
ID: 17772617
   Private Sub OpenXML(ByVal SourceFileName As String) ' Open XML document and run query based on XML node <Type></Type>

        Dim XmlDoc As New XmlDocument
        XmlDoc.Load(SourceFileName)
        Dim currentNode As XmlNode
        Dim currentNodes As XmlNodeList = XmlDoc.SelectNodes("//Type/Type")
        Dim currentArray As ArrayList = NodesToArrayList(currentNodes)

        For Each currentNode In currentNodes
            Select Case currentNode.ToString
                Case "Hardware"
                    QueryHardware(SourceFileName)
                Case "Software"
                    QuerySoftware(SourceFileName)
                Case "SoftwareUnchecked"
                    QueryUnchecked(SourceFileName)
            End Select
        Next

    End Sub

    Private Sub QuerySofware(ByVal SourceFileName As String) 'Insert XML software to database

        Dim Server As String = "localhost", DataBaseName As String = "DB", User As String = "root", Password As String = "letmein"
        Dim ODBCConnectString As String = "DRIVER={MySQL ODBC 3.51 Driver};SERVER=" & Server & ";DATABASE=" & DataBaseName & ";UID=" & User & ";PASSWORD=" & Password & ";OPTION=3;"

        Dim currentXML As New XmlDocument
        currentXML.Load(SourceFileName)
        MessageBox.Show(SourceFileName)

        Dim currentNodes As XmlNodeList = currentXML.SelectNodes("//Program/Program")
        Dim currentArray As ArrayList = NodesToArrayList(currentNodes)

        '------------ Loop to add array to database --------------'

        'Open command prompt to execute the query
        Dim ConnectionObject As New System.Data.Odbc.OdbcConnection(ODBCConnectString)

        ConnectionObject.Open()

        'Dim QueryDelete As String = "Delete From Programs"
        'Dim CommandObject2 As New System.Data.Odbc.OdbcCommand(QueryDelete.ToString.Trim, ConnectionObject)
        'CommandObject2.ExecuteNonQuery()

        'Loop to add each array member to database
        For Each program As String In currentArray

            Dim SQLQuery As String = "Insert Into DB (programs) VALUES ('"
            SQLQuery &= program ' concenate the program (val from array) to the sql statement
            SQLQuery &= "')" ' close the sql statement
            Dim CommandObject As New System.Data.Odbc.OdbcCommand(SQLQuery.ToString.Trim, ConnectionObject)
            CommandObject.ExecuteNonQuery()

        Next

        'Close the database
        ConnectionObject.Close()
        ConnectionObject = Nothing

        MessageBox.Show("Database query complete")
        GsStatus = "Waiting for next file"
        Timer1.Start()

    End Sub
0
 
LVL 9

Accepted Solution

by:
DjDezmond earned 50 total points
ID: 17772635
Private Sub QuerySofware(ByVal SourceFileName As String) 'Insert XML software to database

Did you mean to miss-spell QuerySoftware? Could that be the problem?
0
 
LVL 1

Author Comment

by:nickmarshall
ID: 17772648
Urrgh!!!!!!!!!!!!!!!!!!!!!!!

Spelling error, working now DUH!!!!!

 :-/
0
 
LVL 9

Expert Comment

by:DjDezmond
ID: 17772657
lol, happens to the best of us mate ;)
0
 
LVL 1

Author Comment

by:nickmarshall
ID: 17772706
Ah, thats good to know! :-)
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question