I can not access all the tables in my sql database - mysql

Please can somebody help me with this problem.
I have a Form in VBA and from this from I want to run some SQL queries and retrieve the data from my database and then place the result in a Text box in the Form(GUI)
I have about 20 tables in my Database but surprisingly to me I am only able to access the first table (InventoryCompleteList).
Every attempt to access another table returns an error saying for example (Invalidobject name 'functionalTestResults') whereas when I run the query for (inventoryCompleteList), it seems to be working. Note functionalTestResults and inventoryCompleteList are both tables in my sql server
Please can anyone help me figure out what the problem might be.
Below is the view of my DB
`Private Sub ENTER_Click()
Dim cn As Object
Dim rs As Object
Dim strFile As String
Dim strCon As String
Dim strSQL, strInput As String
strCon = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security
Info=True;Initial Catalog=KBOW;Data Source=10.23.30.8\KBOW;Use Procedure
for Prepare=1;Auto Translate=True;Packet Size=4096;Workstation
ID=ULWW101;Use Encryption for Data=False;Tag with column collation when
possible=False;"
Set cn = CreateObject("ADODB.Connection")
cn.Open strCon
strSQL = "SELECT Date,Location FROM functionalTestResults;"
'Added the following four lines
Set rs = CreateObject("ADODB.RECORDSET")
rs.ActiveConnection = cn
rs.Open strSQL
Sheet1.Range("A1").CopyFromRecordset rs
Result.Text = "CopyFromRecordset rs"
'removed
'cn.Execute strSQL
rs.Close
cn.Close
Set cn = Nothing
End Sub
`

You need to be granted "Select" on those tables, as it seems you currently do not have this permission.
Speak with your dba about being granted access to this table.

Related

create linked tables with vba through odbc connection (MySql) in excel

I have an excel file, which needs to have a connection to a mysql database with odbc. I am aware of the option to do it through UI, but i would prefer not to (data - get_data - from other sources - from odbc). I have made it in access, the linked tables are the same as the ones that i can create through the UI. In excel, i have not found a solution for this.
For now, i have some weird code that establishes the connection just fine, but creates weird tables in excel when trying to get the data from the database (numbers being converted to dates, some data not even loading, ...).
Here is the code that i have for now in excel:
Function connect2mssql()
Dim con As ADODB.connection
Dim rs As ADODB.Recordset
Set con = New ADODB.connection
Dim SQL As String
con.Open "DRIVER={MySQL ODBC 5.3 Unicode Driver};SERVER=192.168.12.192;DATABASE=rezervni_deli;UID=vzdrzevalec;PWD=unichem"
''####################
SQL = "SELECT * FROM rezervni_deli;"
Set rs = New ADODB.Recordset
rs.Open SQL, con
For intColIndex = 0 To rs.Fields.Count - 1
Worksheets("rezervni_deli").Range("A1").Offset(0, intColIndex).Value = rs.Fields(intColIndex).Name
Next
Worksheets("rezervni_deli").Range("A2").CopyFromRecordset rs
''####################
SQL = "SELECT * FROM p_linija;"
Set rs = New ADODB.Recordset
rs.Open SQL, con
For intColIndex = 0 To rs.Fields.Count - 1
Worksheets("p_linija").Range("A1").Offset(0, intColIndex).Value = rs.Fields(intColIndex).Name
Next
Worksheets("p_linija").Range("A2").CopyFromRecordset rs
''####################
rs.Close
Set rs = Nothing
End Function

copy data form sqlserver database to access database and vice versa

I have built a program by ms access as a front and save DATA IN sql server database. Also I have some local table in my program.
My program is connected to sql server by connection string and i can read, write, delete and update data.Sometimes I need to copy result of a query to my local table into access and sometimes I want to append one of my access table to sql table
I have written a connection and tried to executed it like this:
Function CopyData()
Dim cn as ADODB.Connection
Dim strServer, strDatabase, strUsername, strPassword As String
Dim strConnectionString As String
strServer = "10.25.2.120"
strDatabase = "dbKala"
strUsername = "javid"
strPassword = "1234"
strConnectionString = "Provider=SQLOLEDB;Data Source=" & strServer & ";Initial Catalog=" & strDatabase & ";User ID=" & strUsername & ";Password=" & strPassword & ";"
Set cn = New ADODB.Connection
cn.ConnectionString = strConnectionString
cn.CommandTimeout = 0
cn.Open
cn.Execute "INSERT INTO GetTelServer Select * FROM dbo.telefone"
End Function
but data isn't copied from sql to access and show me a message about invalid object my access table
I need to help me how to copy a query from sql to access table and vice verse
This task would be a lot easier with linked DAO.Tables, but that needs proper ODBC-Driver for SQL-Server.
If Ms-Access and SQL-Servers bitness match (x64) you can try using OPENROWSET on SQL-Server to access Ms-Access tables from there.
E.g
INSERT INTO SqlServerTable (SqlServerField) SELECT AccessField FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0',
'C:\Program Files\Microsoft Office\OFFICE11\SAMPLES\Northwind.mdb';
'admin';'',AccessTable);
If bitness doesn't match, you have to create 2 different connections and use recordsets (or Action-Query for insert)
One rs is to select the data, second is for inserts:
Dim cn As ADODB.Connection, rs As ADODB.Recordset
Dim cn2 As ADODB.Connection, rs2 As ADODB.Recordset
Set cn = New ADODB.Connection
cn.Open "Provider=SQLNCLI11;Server=server;Database=db;Trusted_Connection=yes;"
Set cn2 = New ADODB.Connection
cn2.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Path\To\AccessDb;"
Set rs = cn.Execute("SELECT SqlServerField FROM SQLSERVERTable")
Set rs2 = cn2.Execute("SELECT AccessField FROM AccessTable")
Do Until rs2.Eof
rs.AddNew
rs.Fields("SqlServerField").Value = rs2.Fields("AccessField").Value
rs.Update
rs2.MoveNext
Loop
rs.Close
rs2.Close
cn.Close
cn2.Close
Of course the fields data-types have to be compatible.

numeric field overflow Attempting to import Lotus123 Sheets in to MS Access

I am working on a project where the company still has everything in lotus notes and we're starting redesign the system so they no longer run on Lotus.
Now, I am just trying to import the Lotus tables in an MS Access Database but keep getting the error "Numeric Overflow". Does anyone know how to handle this?
The table has 196 columns and 846 rows. Currently, I have named a range in the lotus table. However, ideally we would be able to just name the range we want to import so:
"SELECT * INTO PRNTDATM FROM [a:a1..a:a8100]"
Full error:
Run-time error '-2147467259(800004005)' Numeric Field overflow
Private Sub Command0_Click()
Dim CombLoop As Integer
Dim LotusCn As Object
Dim rsLotus As Object
Dim strSql, CombFileName, GotoRange As String
Set LotusCn = CreateObject("ADODB.Connection")
Set rsLotus = CreateObject("ADODB.Recordset")
LotusCn.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source= K:\GEVS04\NUMBDATM.WK3; Extended Properties=Lotus WK3;"
strSql = "Select * INTO PRNTDATM FROM phil"
rsLotus.Open strSql, LotusCn
LotusCn.Close
Set rsLotus = Nothing
Set LotusCn = Nothing
End Sub
Since this Post i have been working on it and had some improvements i can now read and import some test data to the tables. Found appending worked better than importing whole new table so will have to try that route. Doing this seems to have fixed the overflow issue because i now have the table structure itself already in place as there are only about 5 Table structures that should work fine for this code.
The code is now:
Dim CombLoop As Integer
Dim LotusCn As Object
Dim rsLotus As Object
Dim strSql As String
Dim rs As DAO.Recordset
Set LotusCn = CreateObject("ADODB.Connection")
Set rsLotus = CreateObject("ADODB.Recordset")
LotusCn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\Users\germany5.PLIMSOLL\Desktop\TEST.WK4;" & _
"Extended Properties=Lotus WK4;"
strSql = "SELECT * FROM [a1..C3];"
rsLotus.Open strSql, LotusCn
Set rs = CurrentDb.OpenRecordset("Select * From NICHDATMGECO20;")
If Not (rsLotus.EOF And rsLotus.BOF) Then
FindRecordCount = rsLotus.RecordCount
rsLotus.MoveFirst
Do Until rsLotus.EOF = True
Field1 = rsLotus![Reg. Number]
Field3 = rsLotus![Sales / Tot.Assets]
Field2 = rsLotus![Company Name]
rs.AddNew
rs![Reg. Number] = Field1
rs![Company Name] = Field2
rs.Update
rsLotus.MoveNext
Loop
End If
LotusCn.Close
Set rsLotus = Nothing
Set LotusCn = Nothing
rs.Close
Just to let you know the problem has been solved and above is the end result of the code which works fine for me. I did need to reference Lotus 123 in Tools just in case anyone wants to replicate what Ive done here.

Access VBA - ADO Recordset - No value given for one or more required parameters

I've run across this error when attempting to query the database with ADO. From searching around I've found that this probably means there is an issue with the SQL that I am using.
Dim rs As Object
Dim varGetRows As Variant
Dim sqlStr As String
sqlStr = "SELECT qryEmpRewardsDetail.PBRID, qryEmpRewardsDetail.ActualReward, qryEmpRewardsDetail.ProratedActual, qryEmpRewardsDetail.Adjust, qryEmpRewardsDetail.Total, qryEmpRewardsDetail.Prorated FROM qryEmpRewardsDetail WHERE qryEmpRewardsDetail.ReviewYearID=8 AND qryEmpRewardsDetail.EmployeeID=30 AND qryEmpRewardsDetail.TypeID=1;"
Set rs = CreateObject("ADODB.Recordset")
rs.Open sqlStr, CurrentProject.Connection
varGetRows = rs.GetRows()
I've re-written it in a couple of different ways, checked the spelling a hundred times and copied and pasted it into an access query which ran fine.
For the life of me I can't figure out what could be wrong with it. Any ideas on what it could be or some suggestions on how to narrow down the issue?
Thanks!
Does this run?
SELECT * FROM qryEmpRewardsDetail WHERE qryEmpRewardsDetail.ReviewYearID=8 AND qryEmpRewardsDetail.EmployeeID=30 AND qryEmpRewardsDetail.TypeID=1
If it does, then you likely spelt a field wrong, add one by one till happy.
Otherwise, try removing each WHERE condition and test between each.
Define your recordset fully just to be safe:
Dim rst As ADODB.Recordset
Set rst = New ADODB.Recordset
rst.ActiveConnection = CurrentProject.Connection
rst.CursorType = adOpenDynamic
rst.LockType = adLockReadOnly
rst.Open sqlStr
If rst.BOF and rst.EOF Then
'no records returned
End If
Do While Not rst.EOF
'Doing stuff with row
rst.MoveNext
Loop

how to fill a recordset in vba in a module behind a db

I have tried many ways to get the job done. I am inexperienced with the
Access VBA.
I think the problem is how to set the current database. The code is in a module from another db as the current db. I have paste the code here in a module behind the
currentdb that also gives the same error. I have looked after very much questions.
It must be simple. But I don't see the answer.
Private Sub project()
Dim projectnamen As DAO.Database
Dim strSQL As String
Dim rs As DAO.Recordset
Dim strdbName As String
Dim strMyPath As String
Dim strdb As String
Dim accapp As Object
Path = "c:\GedeeldeMappen\programma en bestanden stiko"
strdbName="projektnamen.accdb"
strMyPath = Path
strdb = strMyPath & "\" & strdbName
'make the db "projectnamen"current. Perhaps this is possible with set??
Set accapp = CreateObject("Access.Application")
accapp.OpenCurrentDatabase (strdb)
'fieldname is naam_van_het_project
'tablename is projectnaam
strSQL = "SELECT All naam_van_het_project FROM projectnaam;"
'here i get an error "can't find the object Select All naam_van_het_project
'FROM projectnaam" error 3011
Set rs = CurrentDb.OpenRecordset(strSQL, dbOpenTable)
rs.MoveFirst
Do While Not rs.EOF
MsgBox (rs)
rs.MoveNext
Loop
End Sub
I think you want to run that query against the db which you opened in the new Access session, accapp.
The CurrentDb method is a member of Application. So qualify CurrentDb with the object variable name of that other application session.
'Set rs = CurrentDb.OpenRecordset(strSQL, dbOpenTable)
Set rs = accapp.CurrentDb.OpenRecordset(strSQL, dbOpenSnapshot)
Note OpenRecordset won't let you use dbOpenTable with a query. So I arbitrarily chose dbOpenSnapshot instead. If that's not what you want, substitute a different constant from the RecordsetTypeEnum Enumeration (see Access help topic for details).