Data Filter SAS VBA - excel

data.Filter = "(Dec = 'Okay' AND no = '001' AND date >=" &
Sheets("parameter").range("A1") & ")" & _"or (Dec = 'Okay' AND
(criteria = 'TOM1' OR criteria = 'TOM2' OR criteria = 'TOM3') AND
date >=" & Sheets("parameter").range("A1") & ")"
Why does it show a run time error? I believe the problem starts from the filter consisting "criteria". How do I make things right?
NOTE: By adding additional line is no longer allowed as the maximum number of line is at 24, and i already exceeded it.

It is probably because of the date values you're passing in from Sheets("parameter").range("A1").
Assuming you're passing in a value from A1 of 20JAN2015 for example, you could try something along the lines of the below:
data.Filter = "(Dec = 'Okay' AND no = '001' AND date >='" &
Sheets("parameter").range("A1") & "'d)" & "or (Dec = 'Okay' AND
(criteria = 'TOM1' OR criteria = 'TOM2' OR criteria = 'TOM3') AND
date >='" & Sheets("parameter").range("A1") & "'d)"
This would give:
(Dec = 'Okay' AND no = '001' AND date >='20JAN2015'd) or (Dec = 'Okay' AND
(criteria = 'TOM1' OR criteria = 'TOM2' OR criteria = 'TOM3') AND
date >='20JAN2015'd)
Compare this with:
data _null_ ;
datetxt='20JAN2015' ;
datenum='20JAN2015'd ;
put "Date as text " datetxt= ;
put "Date as SAS date " datenum= ;
run ;

Related

Add single quotes around a word in an excel column

Currently the text in a cell looks like this-
UPDATE A SET group_name = B WHERE group_ID = C;
Formula used for this is-
="UPDATE "&$N$1&" SET "&$N$2&" = "&TEXT($B2,"MM/DD/YY")&"WHERE "&$N$3&" = "&$E2&";"
I want to add single quotes around B here so that it looks like this-
UPDATE A SET group_name = 'B' WHERE group_ID = C;
How do I achieve this by tweaking the above mentioned formula?
As commented few methods here. All following formulas should work for you.
="UPDATE "&$N$1&" SET "&$N$2&" = "&TEXT($B2,"'MM/DD/YY'")&" WHERE "&$N$3&" = "&$E2&";"
="UPDATE "&$N$1&" SET "&$N$2&" = '"&TEXT($B2,"MM/DD/YY")&"' WHERE "&$N$3&" = "&$E2&";"
="UPDATE "&$N$1&" SET "&$N$2&" = "& CHAR(39) & TEXT($B2,"MM/DD/YY") & CHAR(39) & " WHERE "&$N$3&" = "&$E2&";"

SQLClient Command Parameter Query String Length

I am connecting to a SQL Server and am trying to limit the results by adding parameters. The first parameter I added, #sdate, worked just fine. But, now I am trying to add a second parameter which is not working. I want the field, LP_EOC_DATA.PL, to only be returned if the length of the string is greater than 6 characters long. The code below executed, and like I say, the dates returned were correct, but it also returned values from LP_EOC_DATA.PL that had string lengths less than 6. Please let me know if you know how to get this to work. Thanks in advance.
Sub doSQL()
Dim myConn As SqlConnection
Dim myCmd As SqlCommand
Dim myReader As SqlDataReader
Dim sqlString As String = "SELECT LP_EOC_DATA.PL as PLs, LP_EOC_DATA.cDate as ReadDate, LP_EOC_LOV.LOCATION as Location " &
"FROM LP_EOC_DATA INNER JOIN LP_EOC_LOV ON LP_EOC_DATA.PIC = LP_EOC_LOV.PIC " &
"WHERE LP_EOC_DATA.cDate > (#sdate) AND LEN(LP_EOC_DATA.PL) > #slen1 " &
"UNION SELECT dbo.VT_DATA.PL as PLs, dbo.VT_DATA.cDate as ReadDate, dbo.VT_LOV.LOCATION as Location " &
"FROM dbo.VT_DATA INNER JOIN dbo.VT_LOV ON dbo.VT_DATA.PIC = dbo.VT_LOV.PIC " &
"WHERE dbo.VT_DATA.cDate > (#sdate) AND LEN(dbo.VT_DATA.PL) > #slen1 " &
"ORDER BY ReadDate;"
myConn = New SqlConnection("SERVER=ServerName;UID=uName;" &
"PWD=Password;")
myCmd = myConn.CreateCommand
myCmd.CommandText = sqlString
myCmd.Parameters.AddWithValue("#sdate", DateTimePicker1.Value)
myCmd.Parameters.AddWithValue("#slen1", 6)
'myCmd.Parameters.AddWithValue("#rx1", "'%[^0-9a-z]%'")
'myCmd.Parameters.AddWithValue("#rx2", " dbo.VT_DATA.PL NOT LIKE '%[^0-9a-z]%'")
myConn.Open()
myReader = myCmd.ExecuteReader()
Table.Load(myReader)
DataGridView1.Visible = True
DataGridView1.DataSource = Table
lblTotal.Text = Table.Rows.Count
End Sub
Also, as you can see, I am looking to add another parameter that only returns alphanumeric results from the same LP_EOC_DATA.PL field. I haven't got quite that far yet, but if you see something I'm doing wrong there too, I'd appreciate the input.
It helps if you format your SQL a little more. There's some structure, but it still comes off as a big wall of text. It's even harder for us to debug than it is for you, since we don't know your schema at all. There are also a number of other little things you should do different before we even address the question (Using block so connection is closed in case of exception, avoid AddWithValue() for index safety, isolate SQL from user interface, etc):
Function doSQL(StartDate As DateTime) As DataTable
Dim result As New DataTable
Dim sqlString As String = _
"SELECT LP_EOC_DATA.PL as PLs, LP_EOC_DATA.cDate as LPRReadDate, LP_EOC_LOV.LOCATION as Location " &
"FROM LP_EOC_DATA " &
"INNER JOIN LP_EOC_LOV ON LP_EOC_DATA.PIC = LP_EOC_LOV.PIC " &
"WHERE LP_EOC_DATA.cDate > #sdate AND LEN(COALESCE(LP_EOC_DATA.PL,'')) > #slen1 " &
"UNION " &
"SELECT dbo.VT_DATA.PL as PLs, dbo.VT_DATA.cDate as ReadDate, dbo.VT_LOV.LOCATION as LPRLocation " &
"FROM dbo.VT_DATA " &
"INNER JOIN dbo.VT_LOV ON dbo.VT_DATA.PIC = dbo.VT_LOV.PIC " &
"WHERE dbo.VT_DATA.cDate > #sdate AND LEN(COALESCE(dbo.VT_DATA.PL,'')) > #slen1 " &
"ORDER BY ReadDate;"
Using myConn As New SqlConnection("SERVER=ServerName;UID=uName;" &
"PWD=Password;"), _
myCmd As New SqlCommand(sqlString, myConn)
myCmd.Parameters.Add("#sdate", SqlDbType.DateTime).Value = StarDate
myCmd.Parameters.Add("#slen1", SqlDbType.Int).Value = 6
myConn.Open()
result.Load(myCmd.ExecuteReader())
End Using
Return result
End Function
And then call it like this:
Dim tbl As DataTable = doSql(DateTimePicker1.Value)
DataGridView1.Visible = True
DataGridView1.DataSource = tbl
lblTotal.Text = tbl.Rows.Count
As for the question, there are a few possibilities: NULL values can give unexpected results in this kind of situation (the code I posted already accounts for that). You may also have trouble with certain unicode whitespace padding your character count. Another possibility is char or nchar fields instead of varchar or nvarchar, though I don't think that's the issue here.
This is not an answer to the question per se but a reply to the request for an XML literal example. As that requires a few lines of code, I'd rather not put it in a comment.
Dim sql = <sql>
SELECT *
FROM MyTable
WHERE MyColumn = #MyColumn
</sql>
Dim command As New SqlCommand(sql.Value, connection)
Note that the element name can be anything you want but I usually use 'sql' when it's for SQL code.

How do I limit the values I can save?

I'm currently working on a Registration form where all the details of a student are save in a text file.
In one of my fields, I have a combobox list of all the school he/she can select.
I populated the combobox using a textfile.
The format of these values are for example: (code~school name) SCH001~Saint Thomas College
Question - how do I limit the values I can save?
example - I want only the school code to be saved without the name of school: SCH001
Here's how I save the fields in a text file:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnRegister.Click
Dim firstname, lastname, email, mobile, level, currentschool, currenttrack, institution1, institution2, institution3, institution4, institution5, institution6, courses1, courses2, courses3 As String
firstname = txtFName.Text
lastname = txtLName.Text
email = txtEmail.Text
mobile = txtMobile.Text
level = cmbLevel.Text
currentschool = cmbCurrentSchool.Text
currenttrack = cmbCurrentTrack.Text
institution1 = cmbInstitution1.Text
institution2 = cmbInstitution2.Text
institution3 = cmbInstitution3.Text
institution4 = cmbInstitution4.Text
institution5 = cmbInstitution5.Text
institution6 = cmbInstitution6.Text
courses1 = cmbCourse1.Text
courses2 = cmbCourse2.Text
courses3 = cmbCourse3.Text
Using sw As StreamWriter = File.AppendText("C:\Users\jmrosales\Documents\RegistrationForm\Registered.txt")
sw.WriteLine(firstname & "~" & lastname & "~" & email & "~" & mobile & "~" & level & "~" & currentschool & "~" & currenttrack & "~" & institution1 & "~" & institution2 & "~" & institution3 & "~" & institution4 & "~" & institution5 & "~" & institution6 & "~" & courses1 & "~" & courses2 & "~" & courses3)
End Using
MsgBox("Registration Complete!")
End Sub
I hope you guys understand what i'm trying to say.
I'm just new here.
You can do this in two ways:
Using .Split()
Using .Substring()
Using .Split()
Here's what your code will look like:
currentschool = cmbCurrentSchool.Text.Split("~")
This will return two strings, "SCH001" and "Saint Thomas College".
But since you need only the Code, put (0) at the end of the code above:
currentschool = cmbCurrentSchool.Text.Split("~")(0)
Further you can add c in the above snippet like:
currentschool = cmbCurrentSchool.Text.Split("~"c)(0)
This would specify ~ is a character.
using .Substring()
This will only work if it's sure that your code will always be 6 character (3 for SCH and rest 3 for the number-code)
currentschool = cmbCurrentSchool.Text.Substring(0,6)
Here, 0 means start picking the character from 1st position and 6 means get six characters to get a new string.
Hope this helps :)
Presuming that the school code would follow exactly 6 characters as in this SCH001~Saint Thomas College, you can use Substring:
currentschool = cmbCurrentSchool.Text.Substring(0,6) '6 means 6 characters to be cut from cmbCurrentSchool to currentschool
If here currentschool stores the schoolcode.
Else, you can use Split:
currentschool = cmbCurrentSchool.Text.Split("~")(0)
The most simple answer would be to split the string at ~ and just return the part you need:
If cmbCurrentSchool.Text.Contains("~") Then 'A check to avoid possible errors.
currentschool = cmbCurrentSchool.Text.Split("~"c)(0) '0 means that we should get the first item of the array, thus "SCH001".
Else
currentschool = cmbCurrentSchool.Text
End If
cmbCurrentSchool.Text.Split("~"c) will return an array of two strings:
"SCH001~Saint Thomas College" -> {"SCH001", "Saint Thomas College"}
Documentation:
String.Split() method

How to convert part of a cell value to bold

I have the below VBA code and A and B are holding some strings. I want to concatenate these values with some other strings and store the result in a different cell, but I want only the strings in A and B to be formatted as bold and the rest as normal text.
Set A = Worksheets("Mapping").Cells(rowNumber, columnNumber)
Set B = Worksheets("Mapping").Cells(rowNumber, 3)
' E.g.: A="currency", B="Small Int"
Worksheets("TestCases").Cells(i, 2) = "Verify the column " & A & " has same Data type " & B & " in code as well as Requirement document"
Expected output:
Verify the column currency has same Data type Small Int in code as well as Requirement document
Note: The values of A and B keep changing, so we cannot use the Characters() function.
Any help will be highly appreciated.
You can use the Characters() method - you just need to keep track of the length of the substrings. Personally, I would store the static strings in variables so that I can change them later without having to recalculate the indexes by hand:
' Untested
Set A = Worksheets("Mapping").Cells(rowNumber, columnNumber)
Set B = Worksheets("Mapping").Cells(rowNumber, 3)
Dim S1 = "Verify the column "
Dim S2 = " has same Data type "
Dim S3 = " in code as well as Requirement document"
With Worksheets("TestCases").Cells(i, 2)
.Value = S1 & A & S2 & B & S3
.Characters(Len(S1), Len(A)).Font.Bold
.Characters(Len(S1)+Len(A)+Len(S2), Len(B)).Font.Bold
End With
The function to change the font style is:
[Cells/Cell range].Font.FontStyle = "Bold"
Therefore something like might work:
Worksheets("Mapping").Cells(rowNumber, columnNumber).Font.FontStyle = "Bold"
You can also make things have underlines, strikethroughs etc... I found this really helpful blog post which goes through everything you should need to know:
http://software-solutions-online.com/excel-vba-formating-cells-and-ranges/#Jump4
I think you should have searched for this information yourself... Nevertheless this is the code that you should use to convert some cell data to bold:
Worksheets("Mapping").Cells(rowNumber, columnNumber).Font.Bold = True

Dynamic excel chart doesn't display all of the data

I have this code that allows users to enter chart parameters into some cells and dynamically create a chart. Many series (up to four) are allowed on two vertical (y) axis and one shared horizontal (x) axis. The chart is a mixture of columns and lines normally, and the data ranges are of varying length. I have this code that adds the series like so (I'll try to stick to what I believe is the relevant code)
seriesCount = 1
If hasSeries1 = True Then
ActiveChart.SeriesCollection.NewSeries
ActiveChart.SeriesCollection(seriesCount).Name = .Cells(2, 6) & " " & axisside1
ActiveChart.SeriesCollection(seriesCount).ChartType = chartType1
ActiveChart.SeriesCollection(seriesCount).AxisGroup = axisgroup1
ActiveChart.SeriesCollection(seriesCount).Border.LineStyle = borderStyle1
ActiveChart.SeriesCollection(seriesCount).Border.Color = lineColor1
ActiveChart.SeriesCollection(seriesCount).Format.Line.Weight = lineWidth1
ActiveChart.SeriesCollection(seriesCount).Format.Fill.ForeColor.RGB = seriesColor1
ActiveChart.SeriesCollection(seriesCount).Format.Line.Visible = hasLine1
ActiveChart.SeriesCollection(seriesCount).XValues = dates1
ActiveChart.SeriesCollection(seriesCount).Values = dataset1
seriesCount = seriesCount + 1
End If
If hasSeries2 = True Then
ActiveChart.SeriesCollection.NewSeries
ActiveChart.SeriesCollection(seriesCount).Name = .Cells(3, 6) & " " & axisside2
ActiveChart.SeriesCollection(seriesCount).ChartType = chartType2
ActiveChart.SeriesCollection(seriesCount).AxisGroup = axisgroup2
ActiveChart.SeriesCollection(seriesCount).Border.LineStyle = borderStyle2
ActiveChart.SeriesCollection(seriesCount).Border.Color = lineColor2
ActiveChart.SeriesCollection(seriesCount).Format.Line.Weight = lineWidth2
ActiveChart.SeriesCollection(seriesCount).Format.Fill.ForeColor.RGB = seriesColor2
ActiveChart.SeriesCollection(seriesCount).Format.Line.Visible = hasLine2
ActiveChart.SeriesCollection(seriesCount).XValues = dates2
ActiveChart.SeriesCollection(seriesCount).Values = dataset2
seriesCount = seriesCount + 1
End If
If hasSeries3 = True Then
ActiveChart.SeriesCollection.NewSeries
ActiveChart.SeriesCollection(seriesCount).Name = .Cells(4, 6) & " " & axisside3
ActiveChart.SeriesCollection(seriesCount).ChartType = chartType3
ActiveChart.SeriesCollection(seriesCount).AxisGroup = axisgroup3
ActiveChart.SeriesCollection(seriesCount).Border.LineStyle = borderStyle3
ActiveChart.SeriesCollection(seriesCount).Border.Color = lineColor3
ActiveChart.SeriesCollection(seriesCount).Format.Line.Weight = lineWidth3
ActiveChart.SeriesCollection(seriesCount).Format.Fill.ForeColor.RGB = seriesColor3
ActiveChart.SeriesCollection(seriesCount).Format.Line.Visible = hasLine3
ActiveChart.SeriesCollection(seriesCount).XValues = dates3
ActiveChart.SeriesCollection(seriesCount).Values = dataset3
seriesCount = seriesCount + 1
End If
If hasSeries4 = True Then
ActiveChart.SeriesCollection.NewSeries
ActiveChart.SeriesCollection(seriesCount).Name = .Cells(5, 6) & " " & axisside4
ActiveChart.SeriesCollection(seriesCount).ChartType = chartType4
ActiveChart.SeriesCollection(seriesCount).AxisGroup = axisgroup4
ActiveChart.SeriesCollection(seriesCount).Border.LineStyle = borderStyle4
ActiveChart.SeriesCollection(seriesCount).Border.Color = lineColor4
ActiveChart.SeriesCollection(seriesCount).Format.Line.Weight = lineWidth4
ActiveChart.SeriesCollection(seriesCount).Format.Fill.ForeColor.RGB = seriesColor4
ActiveChart.SeriesCollection(seriesCount).Format.Line.Visible = hasLine4
ActiveChart.SeriesCollection(seriesCount).XValues = dates4
ActiveChart.SeriesCollection(seriesCount).Values = dataset4
End If
Here is the problem: the chart only displays part of the data it is supposed to. When I right-click on the data series, hit Select Data and choose Edit, the correct series (both x and y) become highlighted, but what is being shown is a truncated subset of what should be there.
Here is a sample of what I'm seeing
Here is some of the data for the light blue column
12/30/2005 307%
1/31/2006 302%
2/28/2006 248%
3/31/2006 262%
4/28/2006 285%
5/31/2006 256%
... ...
... ...
... ...
6/30/2014 147%
Notice how this data should be showing on the chart beginning at 12/30/2005, but it's starting at 11/30/2013 instead (though the values appear to be correct, 307%, 302%, etc.). It is almost as though excel is forcing the 2nd and 3rd data series to be the same length as the first one. The first one is charting correctly.
I think you're not using a XY chart, thus you must have the same labels (Xvalues = dates for you) for every series. That means that you need to create a unique dates-set containing all the dates and assign it (as Xvalues) to the first serie.

Resources