Monday, January 31, 2011

If Statements

A few different applications of If statements and Case satements

An if statement that makes one decision
 Public Class Form1
  
   Private Sub btnOne_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOne.Click
  
     If chkCheck.Checked Then
  
       MessageBox.Show("Congrats, You Are Alive")
  
     End If
  
   End Sub
  
 End Class  

An if statement where it's either or
 Public Class Form1
  
   Private Sub btnOne_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOne.Click
  
     If chkCheck.Checked Then
  
       MessageBox.Show("You Win")
  
     Else
  
       MessageBox.Show("You Lose")
  
     End If
  
   End Sub
  
 End Class  

An if statement where many decisions are made
 Public Class Form1
  
   Private Sub btnOne_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOne.Click
  
     If chkCheck.Checked Then
  
       MessageBox.Show("You win 1 dollar")
  
     ElseIf chkCheck2.Checked Then
  
       MessageBox.Show("You lose 1 dollar")
  
     ElseIf chkCheck3.Checked Then
  
       MessageBox.Show("You win 2 dollars")
  
     ElseIf chkCheck4.Checked Then
  
       MessageBox.Show("You lose 2 dollars")
  
     End If
  
   End Sub
  
 End Class  

A case statement
 Public Class Form1
  
   Private Sub btnFeedback_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFeedback.Click
  
     Dim team As String
  
     Dim anwser As String
  
     team = txtYesOrNo.Text
  
     Select Case team
  
       Case "yes"
  
         anwser = "Too Bad"
  
       Case "no"
  
         anwser = "Good Choice"
  
       Case Else
  
         anwser = "Check Your Spelling"
  
     End Select
  
     MsgBox(anwser)
  
   End Sub
  
 End Class  

Sunday, January 30, 2011

VB Word Problems

1. Morgan has 6 cows and 7 sheep. How many total animals does she have?
Public Class frmcows
   Private Sub btnAnwser_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAnwser.Click
  
     Dim sheep As Integer = 7
  
     Dim cows As Integer = 6
  
     Dim total As Integer
  
     total = sheep * cows
  
     MessageBox.Show(total)
  
   End Sub
  
 End Class
  

2. Diane bought 7 burgers, each of which cost $4. How much did she spend on burgers?
   Private Sub btnCost_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCost.Click
  
     Dim cost As Integer = 4
  
     Dim amount As Integer = 7
  
     Dim totalcost As Integer
  
     totalcost = cost * amount
  
     MessageBox.Show(totalcost)
  
   End Sub
  
 End Class
  

3. Ole has 15 apples and 12 oranges. Whats his total fruit count?
Public Class frmFruit
 Private Sub btnFruit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFruit.Click
  
     Dim apples As Integer = 15
  
     Dim oranges As Integer = 12
  
     Dim totalfruit As Integer
  
     totalfruit = apples + oranges
  
     MessageBox.Show(totalfruit)
  
   End Sub
  
 End Class
  

4. There are 33 horses to begin, 15 go into the barn, 7 come back out. What is the new population?

 Public Class frmHorses
  
   Private Sub btnLeftover_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLeftover.Click
  
     Dim beginningpopulation As Integer = 33
  
     Dim barn As Integer = 15
  
     Dim returners As Integer = 7
  
     Dim newpopulation As Integer
  
     newpopulation = beginningpopulation - barn + returners
  
     MessageBox.Show(newpopulation)
  
   End Sub
  
 End Class  

5. Ingelbert has 15 apples and 3 times as many oranges. How many pieces of fruit do they have?
 Public Class frmFruit2
  
   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  
     Dim apples As Integer = 15
  
     Dim oranges As Integer
  
     Dim total As Integer
  
     oranges = apples * 3
  
     total = apples + oranges
  
     MessageBox.Show(total)
  
   End Sub
  
 End Class  

6. Using the grade table decide how many marks did Brian score in math and science combined, how many more does Andrew need for a perfect math score, and what is Andrew's percentages for all o the quizzes put together.
 Public Class frmGradeTABLE
  
   Private Sub btnGradeInfo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGradeInfo.Click
  
     Dim AEnglish As Integer = 12
  
     Dim AGeography As Integer = 19
  
     Dim AMath As Integer = 18
  
     Dim AScience As Integer = 7
  
     Dim BEnglish As Integer = 22
  
     Dim BGeography As Integer = 15
  
     Dim BMath As Integer = 7
  
     Dim BScience As Integer = 22
  
     Dim combinedmath As Integer
  
     Dim mathperfectscore As Integer
  
     Dim APercentage As Integer
  
     Dim totalanwser As String
  
     combinedmath = BMath + AMath
  
     mathperfectscore = 25 - AMath
  
     APercentage = (AEnglish + AGeography + AMath + AScience) / 100
  
     totalanwser = String.Concat(combinedmath.ToString & Environment.NewLine, mathperfectscore.ToString & Environment.NewLine, APercentage.ToString & Environment.NewLine)
  
     Console.WriteLine(totalanwser)
  
     MessageBox.Show(totalanwser)
  
   End Sub
  
 End Class  

Friday, January 28, 2011

Table Variable Create and Inner Join

Below is code on how I created 2 Table Variables and then inner joined the two using a primary key.

 use blog
  
 go
  
 declare @Potatoe_Varieties table
  
 (
  
      Potatoe_ID int identity (1,1) PRIMARY KEY,
  
      Name varchar (40),
  
      Color varchar (10),
  
      Uses varchar (60)
  
 );
  
 Insert into @Potatoe_Varieties
  
 (Name, Color, Uses)
  
 Values
  
 ('Norlands', 'Red', 'Fresh Market Sales')
  
 Insert into @Potatoe_Varieties
  
 (Name, Color, Uses)
  
 Values
  
 ('Russet Burbank', 'Yellow', 'Chips, fries, dehydrated for later use')
  
 Insert into @Potatoe_Varieties
  
 (Name, Color, Uses)
  
 Values
  
 ('Yukon Gold', 'Golden', 'Fresh Market Sales and dehydrated for later use')
  
 declare @Potatoe_size table
  
 (
  
      Potatoe_ID int identity (1,1) PRIMARY KEY,
  
      [weight] varchar (10),
  
      diameter varchar (10),
  
      yield varchar (30)
  
 );
  
 Insert into @Potatoe_size
  
 ([weight], diameter, yield)
  
 Values
  
 ('8oz', '6 inches', '150-300 100lb bags per acre')
  
 Insert into @Potatoe_size
  
 ([weight], diameter, yield)
  
 Values
  
 ('12oz', '7 inches', '250-375 100lb bags per acre')
  
 Insert into @Potatoe_size
  
 ([weight], diameter, yield)
  
 Values
  
 ('7oz', '6 inches', '150-300 100lb bags per acre')
  
 select PV.*, [weight], diameter, yield
  
 from @Potatoe_Varieties PV
  
 inner join @Potatoe_size PS
  
      on PV.Potatoe_ID = PS.Potatoe_ID
  

Wednesday, January 26, 2011

Joining Two Strings

 Public Class Form1
  
 
  
   Private Sub btnJoin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnJoin.Click
  
     Dim start As String = 9
  
     Dim finish As String = 7
  
     Dim together As String = String.Concat(start, finish)
  
     Console.WriteLine(together)
  
 
  
 
  
     MessageBox.Show(together)
  
 
  
   End Sub
  
 End Class  

Converting Into to Dec and Dec to Int

   Private Sub btnDecToInt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDecToInt.Click
  
     Dim value As Integer
  
     Dim quantity As Decimal
  
 
  
     value = 5
  
     quantity = 0.3
  
 
  
     quantity = Convert.ToDecimal(value)
  
     MessageBox.Show(quantity.GetType.ToString)
  
   End Sub
  
 
  
   Private Sub btnIntToDec_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnIntToDec.Click
  
     Dim X As Integer
  
     Dim Y As Decimal
  
 
  
     X = 1
  
     Y = 0.1
  
 
  
     X = Convert.ToInt16(Y)
  
     MessageBox.Show(X.GetType.ToString)
  
   End Sub
  
 End Class  

Monday, January 24, 2011

Assignment1 - Creating Database and table, inserting records, and selecting all records

 create database Frogs
  
 create table Wyoming_Species
  
      (
  
      Frog_ID int identity (1,1) PRIMARY KEY,
  
      Scientific_Name varchar (100),
  
      IUCN_Red_List_Status varchar (50),
  
      Vernacular_Name varchar (50),
  
      Family varchar(30)
  
      );
  
 Insert Into Wyoming_Species
  
 (Scientific_Name, IUCN_Red_List_Status, Vernacular_Name, Family)
  
 Values
  
 ('Bufo baxteri', 'Extinct in the Wild(EW)', 'Wyoming Toad', 'Bufonidae');
  
 Insert Into Wyoming_Species
  
 (Scientific_Name, IUCN_Red_List_Status, Vernacular_Name, Family)
  
 Values
  
 ('Bufo boreas', 'Near Threatened(NT)', 'Western Toad', 'Bufonidae');
  
 Insert Into Wyoming_Species
  
 (Scientific_Name, IUCN_Red_List_Status, Vernacular_Name, Family)
  
 Values
  
 ('Bufo cognatus', 'Least Concern(LC)', 'Great Plains Toad', 'Bufonidae');
  
 Insert Into Wyoming_Species
  
 (Scientific_Name, IUCN_Red_List_Status, Vernacular_Name, Family)
  
 Values
  
 ('Bufo hemiophrys', 'Lese Concern(LC)', 'Canadian Toad', 'Bufonidae');
  
 Insert Into Wyoming_Species
  
 (Scientific_Name, IUCN_Red_List_Status, Vernacular_Name, Family)
  
 Values
  
 ('Bufo woodhousii', 'Least Concern(LC)', 'Woodhouse''s Toad', 'Bufonidae');
  
 Insert Into Wyoming_Species
  
 (Scientific_Name, IUCN_Red_List_Status, Vernacular_Name, Family)
  
 Values
  
 ('Psedacris maculata', 'Least Concern(LC)', 'Boreal Chorus Frog', 'Hylidae');
  
 Insert Into Wyoming_Species
  
 (Scientific_Name, IUCN_Red_List_Status, Vernacular_Name, Family)
  
 Values
  
 ('Rana catesbeiana', 'Least Concern(LC)', 'Bullfrog', 'Ranidae');
  
 Insert Into Wyoming_Species
  
 (Scientific_Name, IUCN_Red_List_Status, Vernacular_Name, Family)
  
 Values
  
 ('Ranna luteiventris', 'Least Concern(LC)', 'Columbia Spotted Frog', 'Ranidae');
  
 Insert Into Wyoming_Species
  
 (Scientific_Name, IUCN_Red_List_Status, Vernacular_Name, Family)
  
 Values
  
 ('Rana pipiens', 'Least Concern(LC)', 'Northern Leopard Frog', 'Ranidae');
  
 Insert Into Wyoming_Species
  
 (Scientific_Name, IUCN_Red_List_Status, Vernacular_Name, Family)
  
 Values
  
 ('Rana sylvatica', 'Least Concern(LC)', 'Wood Frog', 'Ranidae');
  
 Insert Into Wyoming_Species
  
 (Scientific_Name, IUCN_Red_List_Status, Vernacular_Name, Family)
  
 Values
  
 ('Spea bombifrons', 'Least Concern(LC)', 'Plains Spadefoot', 'Scaphiopodidae');
  
 Insert Into Wyoming_Species
  
 (Scientific_Name, IUCN_Red_List_Status, Vernacular_Name, Family)
  
 Values
  
 ('Spea intermontana', 'Least Concern(LC)', 'Great Basin Spreadfoot', 'Scaphiopodidae');
  
 Insert Into Wyoming_Species
  
 (Scientific_Name, IUCN_Red_List_Status, Vernacular_Name, Family)
  
 Values
  
 ('Ambystoma mavortium', ' ', 'Barred Tiger Salamander', 'Ambystomatidae');
  
 select *
  
 from Wyoming_Species  

Create and Use Database

 create database Fields
  
 Use Fields  

Create Table Variable, Insert Records, and Change one Row

 Use BLOG
  
 GO
  
  declare @Fields table
  
      (
  
      Name varchar (25),
  
      County varchar (25),
  
      Crop varchar (25)
  
      );
  
 Insert into @Fields
  
 (Name, County, Crop)
  
 values
  
 ('Droske Quarter', 'Pembina', 'Potatoes');
  
 Insert into @Fields
  
 (Name, County, Crop)
  
 values
  
 ('Nash Quarter', 'Walsh', 'Wheat');
  
 Insert into @Fields
  
 (Name, County, Crop)
  
 values
  
 ('Cambell Half Section', 'Pembina', 'Navy Beans');
  
 Insert into @Fields
  
 (Name, County, Crop)
  
 values
  
 ('Home 90', 'Pembina', 'Sugar Beets')
  
 Insert into @Fields
  
 (Name, County, Crop)
  
 values
  
 ('Lysengen Quarter', 'Pembina', 'Potatoes')
  
 select *
  
 from @Fields
  
 delete from @Fields
  
 where Name='Lysengen Quarter'
  
 select *
  
 from @Fields
  
 update @Fields
  
 set Crop = 'Wheat'
  
 where Name = 'Cambell Half Section'
  
 select *
  
 from @Fields  

Converting A Number Variable to a String

 Public Class Form1
  
 
  
   Private Sub btnConvert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConvert.Click
  
     Dim number As Integer
  
     number = 97
  
 
  
     number.ToString()
  
 
  
     MessageBox.Show(number)
  
 
  
   End Sub
  
 End Class  

Setting sting, boolean, integer, and decimal Variables

 Public Class Form1
  
 
  
   Private Sub btnInterger_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInterger.Click
  
     Dim interger As Integer
  
     interger = 5
  
     MessageBox.Show(interger)
  
 
  
   End Sub
  
 
  
   Private Sub btnDecimal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDecimal.Click
  
     Dim decimalvalue As Decimal
  
     decimalvalue = 10
  
     MessageBox.Show(decimalvalue)
  
   End Sub
  
 
  
   Private Sub btnBoolean_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBoolean.Click
  
     Dim booleanvalue As Boolean
  
     booleanvalue = 15
  
     MessageBox.Show(booleanvalue)
  
   End Sub
  
 
  
   Private Sub btnString_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnString.Click
  
     Dim stringvalue As String
  
     stringvalue = 20
  
     MessageBox.Show(stringvalue)
  
 
  
   End Sub
  
 End Class
  
   

Friday, January 21, 2011

Selecting and Deleting with a Table Variable

 Use BLOG
  
 GO
  
  declare @Fields table
  
      (
  
      Name varchar (25),
  
      County varchar (25),
  
      Crop varchar (25)
  
      );
  
 Insert into @Fields
  
 (Name, County, Crop)
  
 values
  
 ('Droske Quarter', 'Pembina', 'Potatoes');
  
 Insert into @Fields
  
 (Name, County, Crop)
  
 values
  
 ('Nash Quarter', 'Walsh', 'Wheat');
  
 Insert into @Fields
  
 (Name, County, Crop)
  
 values
  
 ('Cambell Half Section', 'Pembina', 'Navy Beans');
  
 Insert into @Fields
  
 (Name, County, Crop)
  
 values
  
 ('Home 90', 'Pembina', 'Sugar Beets')
  
 Insert into @Fields
  
 (Name, County, Crop)
  
 values
  
 ('Lysengen Quarter', 'Pembina', 'Potatoes')
  
 select *
  
 from @Fields
  
 delete from @Fields
  
 where Name='Lysengen Quarter'
  
 select *
  
 from @Fields  

Selecting with a table variable

 Use BLOG
  
 GO
  
  declare @Fields table
  
      (
  
      Name varchar (25),
  
      County varchar (25),
  
      Crop varchar (25)
  
      );
  
 Insert into @Fields
  
 (Name, County, Crop)
  
 values
  
 ('Droske Quarter', 'Pembina', 'Potatoes');
  
 Insert into @Fields
  
 (Name, County, Crop)
  
 values
  
 ('Nash Quarter', 'Walsh', 'Wheat');
  
 Insert into @Fields
  
 (Name, County, Crop)
  
 values
  
 ('Cambell Half Section', 'Pembina', 'Navy Beans');
  
 Insert into @Fields
  
 (Name, County, Crop)
  
 values
  
 ('Home 90', 'Pembina', 'Sugar Beets')
  
 Insert into @Fields
  
 (Name, County, Crop)
  
 values
  
 ('Lysengen Quarter', 'Pembina', 'Potatoes')
  
 select *
  
 from @Fields  

Wednesday, January 19, 2011

Table Variable

Table Variable tool used in SQL when you need a table that will be removed after you are out of the scope.  This way you only use temporary memory.

Syntax for Table Variable:
Use BLOG
declare @TibetanYaks Table(
        YakID int
       ,YakName char (30))


//Special Note of the @ before the table name

Tempory Tables in SQL

Temporary Table is a very useful tool when using SQL Server, and you are only needing a table for this one session of SQL.  After creating the table and filling with the with temporary data you only need for one session, you can use it like any other normal table, but it will be automatically dropped after closing that session of SQL.  The only restraint on this is that you cannot use foreign keys with Temp Tables.

Syntax to Create Temp Table:
create database BLOG
create table #Yaks (
       YakID int
       ,YakName char (30) )

//Special Note to the pound sign(#) infront of table name Yaks

Friday, January 14, 2011