0

I have two Microsoft Access database tables. They are named Historical_Stock_Prices and Balance_Sheets. I need to combine data from each of these tables to create a table called Daily. I need to take the fields Ticker, [Date], and [Close] from Historical_Stock_Prices and I need to take the field Common_Stocks from Balance_Sheets.

I will not be taking every row from the Historical_Stock_Prices and Balance_Sheets though. I will only be taking the rows that are on or before a date selected in a DateTimePicker named dtpDateSelection.

Now the main problem that I have is that Historical_Stock_Prices contains a row for each day. While Balance_Sheets contains a row for each quarter. So for each day in a quarter the figure that comes from Balance_Sheets will be the same.

Here is the code that I have so far:

Dim Date1 As Date = dtpDateSelection.Value

Try
Dim cmd As OleDbCommand = New OleDbCommand("CREATE PROC Daily AS SELECT [H].[Ticker], [H].[Date], [H].[Close], [B].[Common_Stocks] FROM [Historical_Stock_Prices] AS [H] INNER JOIN [Balance_Sheets] AS [B] ON Int(Year([H].[Date])) = Int([B].[Year]) AND Int(Format([H].[Date],'Q')) = Int([B].[Period]) AND CStr([H].[Ticker]) = CStr([B].[Ticker]) WHERE CDate([H].[Date]) = #" & CDate(Date1) & "#", con)
cmd.ExecuteNonQuery()
Catch ex As Exception
Finally
Dim cmda As OleDbCommand = New OleDbCommand("DROP PROCEDURE Daily", con)
cmda.ExecuteNonQuery()
Dim cmdb As OleDbCommand = New OleDbCommand("CREATE PROC Daily AS SELECT [H].[Ticker], [H].[Date], [H].[Close], [B].[Common_Stocks] FROM [Historical_Stock_Prices] AS [H] INNER JOIN [Balance_Sheets] AS [B] ON Int(Year([H].[Date])) = Int([B].[Year]) AND Int(Format([H].[Date],'Q')) = Int([B].[Period]) AND CStr([H].[Ticker]) = CStr([B].[Ticker]) WHERE CDate([H].[Date]) = #" & CDate(Date1) & "#", con)
cmdb.ExecuteNonQuery()
End Try

This code creates the table Daily and the fields Ticker, [Date], [Close] and Common_Stocks within that table. But the code does not load any data into the rows of the table. Any ideas why not?

7
  • 1
    You're just creating a stored proc (twice for some reason). You need to actually run it to get results. Commented Oct 3, 2013 at 18:28
  • 1
    This code creates a query (view) in MS-Access. This code doesn't return anything Commented Oct 3, 2013 at 18:28
  • @YuriyGalanter How would I actually run it? Commented Oct 3, 2013 at 18:29
  • 1
    @gromit1 do you need a stored query? Just Create a new SELECT command as New OleDbCommand("SELECT [H].[Ticker], .... and use a DataAdapter to fill a DataTable Commented Oct 3, 2013 at 18:31
  • 1
    See @Steve answer below Commented Oct 3, 2013 at 18:46

3 Answers 3

1

I'm not sure your code is do what you need it to do. I don't see a CREATE TABLE command that would create the desired tables or a INSERT INTO command that would insert the records into the tables.

Sign up to request clarification or add additional context in comments.

1 Comment

I've changed my SQL statement to reflect @Steve answer. In VB.net the query still returns blank and in MS Access it throws a Syntax error in SQL expression message.
1

Your code tries to create the equivalent of a View. By itself it doesn't return any data. You could return the data from the SELECT statement. Not sure if the syntax is correct but you could try the cmdText directly with MS-Access and verify the results

Dim cmdText = "SELECT [H].[Ticker], [H].[Date], [H].[Close], [B].[Common_Stocks] " & _
              "FROM [Historical_Stock_Prices] AS [H] INNER JOIN [Balance_Sheets] AS [B] " & _
              "ON Year([H].[Date]) = [B].[Year] AND " & _
              "Format([H].[Date],'Q') = [B].[Period] AND " & _
              "[H].[Ticker] = [B].[Ticker] "
              "WHERE [H].[Date] = ?"
Using cmdb = New OleDbCommand(cmdText, con)
    cmdb.Parameters.AddWithValue("@1", CDate(Date1))
    Using da = new OleDbDataAdapter(cmdb)
        Dim dt = new DataTable("Daily")
        da.Fill(dt)
        ' now you have an IN MEMORY DataTable named Daiyly filled with the data '
        ' returned by the SELECT query '
    End Using
End Using

6 Comments

When I tried this code I got this error in vb.net Additional information: Type mismatch in expression.
Take the query as it and put in Query designer of MS-Access. Usually you will be able to find the problem (Probably there is a problem with the Format that returns a string and the Period but it is also possible that other fields used in the join or in the where are not of the exact type expected)
I no longer the error message. But when I load the DataTable into a` DataGridView` the DataGridView is blank.
But the query in MS-Access with the same value for [H].[Date] returns rows? Try to convert Date1 parameter in a true DateTime using Convert.ToDateTime(Date1)
The query in MS-Access throws a Syntax error in SQL expression.
|
1

Looks like you're creating a stored procedure that doesn't actually INSERT any data into the table Daily that you mentioned.

You might want to change the first portion CREATE PROC Daily AS to INSERT INTO dbo.Daily. That way, the data will be appended to the table.

1 Comment

I've changed my SQL statement to reflect @Steve answer. In VB.net the query still returns blank and in MS Access it throws a Syntax error in SQL expression message.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.