Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
Hopefully this is a simple fix problem.
I have a VBA query which is trying to import 100 or so practically identical (same columns, different data) csv files into one table.
whenever i try to run the macro, it doesn't work and gets
invalid syntax error
Public Function Import()
Dim strPathFile As String, strFile As String, strPath As String
Dim strTable As String
Dim blnHasFieldNames As Boolean
blnHasFieldNames = True
strPath = "C:\Users\Jason\Downloads\Riskmodels\"
strTable = "table"
strFile = Dir(strPath & "*.csv")
Do While Len(strFile) > 0
strPathFile = strPath & strFile
DoCmd.TransferText TransferType:=acImportDelim, _
TableName:=strTable, _
FileName:=strPathFile, _
HasFieldName:=True
strFile = Dir()
Loop
End Function
any idea why this doesn't work?
thanks
Named argument should be HasFieldNames:=
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have some code that combines multiple word documents together, however, I would like to alter it to combine powerpoint presentations together.
I'm new to programming and struggle to find the correct sections to change or the correct 'vocab' to use.
If you could help that would be really useful.
My code is
'Code to merge selected documents together
Sub MergeDocs(strInFullNames() As String, strOutFullName As String,
intNoOfFiles As Integer)
Dim wdApp As word.Application
Dim wdDoc As Word.Document
Dim outDoc As Word.Document
Dim w As Integer
Dim bNewInstance As Boolean
'Try to use already running instance of Word
On Error Resume Next
Set wdApp = GetObject(, "word.Application")
On Error GoTo 0
If wdApp Is Nothing Then
Set wdApp = CreateObject("word.application")
bNewInstance = True
End If
Set outDoc = wdApp.Documents.Add
'For w = 0 To UBound(strInFullNames)
If w > 0 Then
' outDoc.Bookmarks("\EndOfDoc").Range.InsertBreak wdSectionBreakNextPage
' End If
' Set wdDoc = Documents.Open(strInFullNames(w))
' objSelection.PasteAndFormat
'wdDoc.Range.Copy
'wdDoc.Close wdDoNotSaveChanges
'outDoc.Bookmarks("\EndOfDoc").Range.Paste
'Next w
outDoc.SaveAs strOutFullName
'only close Word application if instance created for this macro
If bNewInstance Then
wdApp.Quit
End If
MsgBox "Word Document Created"
End Sub
Thanks
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I have a Db for individuals to test on courses they have taken. This Db takes input from an introduction form, selects data from a query, and passes it to the test form. This works great (with many thanks to #xpofer) in that it returns questions randomly, but the answers are always in the same order. This is because the tables and queries the Recordset is based on contains the following columns:
TABLE
ID
CDC
Vol
Question No (Randomized in query)
Section
Question
Ans A
Ans B
Ans C
Ans D
Correct Answer
What I am trying to do is not only present the questions randomly, but have the answers presented randomly also. To do so, I have separated this into two separate tables.
tblRnd_Ques
ID (PK)
CDC
Vol
Question No
Section
Question
tblRnd_Ans
ID
Q_ID (FK)
Answer
Correct (Y/N)
Here are the queries I am trying to use.
qryRnd_Ques
ID
CDC
Vol
Question No.
Section
Question
Rnd_ID (Randomized ID, ascending)
qryRnd_Ans
Rnd_ID (Randomized ID, Ascending)
Q_ID
Answer
Correct (Y/N)
Now for the problem. Is it possible to create a Recordset using a variable from a previous Recordset? Here is the code I currently have:
Private Sub GetAnswers()
Dim rsAns As Recordset
Dim strAns As String
Dim strSQL As String
Dim intQues As Long
Dim isCorr As Boolean
intQues = !ID
strSQL = "Select [ID], [Answer], [Correct] FROM [qryRnd_Ans] WHERE [Q_ID] = " & intQues
Set rsAns = CurrentDb.OpenRecordset(strSQL, dbOpenDynaset)
strAns = !Answer
isCorr = !Correct
MsgBox "Answer: " & strAns _
& Chr(13) & Chr(10) & "Correct: " & isCorr
rsAns.Close
End Sub
I have tried creating this second Recordset (rsAns) directly in the LoadNextQuestion sub where I now call GetAnswers. I kept getting
Run-time error 3464: Data type mismatch in criteria expression
with this code:
strSQL = "Select [ID], [Answer], [Correct] FROM [qryRnd_Ans] WHERE [Q_ID] = '" & intQues & "'"
Set rsAns = CurrentDb.OpenRecordset(strSQL, dbOpenDynaset)
I figured I may need to place this code in a subroutine, hence the call to GetAnswers. With this code, I get a Compile error: Invalid or unqualified reference at the strAns = !Answer line. I have searched many sites, including Microsoft, CNet, bytes.com, as well as Stackoverflow, and it appears as if the syntax is correct, so I am at a loss as to the problem. Any assistance is appreciated.
UPDATE
After much sole- AND Internet searching, I found a solution. I changed the variable values from strAns = !Answer to strAns = rsAns![Answer], and it all fell into place. Many thanks to DanielG and the other sites I searched.
Since it is an int, have you tried taking your single quotes out:
strSQL = "Select [ID], [Answer], [Correct] FROM [qryRnd_Ans] WHERE [Q_ID] = " & intQues
To get all of the answers in the recordset, you can now iterate the recordset:
If Not (rsANS.EOF And rsANS.BOF) Then
Do Until rsANS.EOF = True
' Get the next answer and do something with it
' Me!MyTextBox = rsANS!MyNextAnswer
rsANS.MoveNext
Loop
End If
I have multiple very similar CSV files saved in the same directory which I want to import into Access in one go. I want them to all go to one table.
I did some research, taught myself some VBA basics and ended up with this script:
Public Function Import()
Dim strPathFile As String, strFile As String, strPath As String
Dim strTable As String
Dim blnHasFieldNames As Boolean
blnHasFieldNames = True
strPath = "C:\Downloads\models"
strTable = "ModelData"
strFile = Dir(strPath & "*.csv")
Do While Len(strFile) > 0
strPathFile = strPath & strFile
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, _
strTable, strPathFile, blnHasFieldNames
strFile = Dir()
Loop
End Function
Trying to run the macro doesn't do anything and I'm stuck where to move forward from here.
I followed the same guide to running a macro in this quick minute long video: https://www.youtube.com/watch?v=mXdn7ca2BX4
I'm hoping I just missed a little step somewhere...
Also, I think I need to import each column in text format as one is not returning date/time data correctly.
I have tried to follow some similar questions on here however I do not really understand VBA coding :/
Any help would be great! thanks!
For CSV file files you have to use DoCmd.TransferText:
DoCmd.TransferText TransferType:=acImportDelim, _
TableName:=strTable, _
FileName:=strPathFile, _
HasFieldNames:=True
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
My goal is get the name of all table from my database and put it on my ComboBox. I hadn't used vb.net from years and now I'm dusting off a bit 'of stuff, but I need help because I don't come out. This is my code:
Imports MySql.Data.MySqlClient
Public Class DataIn
Dim myCommand As New MySqlCommand
Dim myAdapter As New MySqlDataAdapter
Dim myData As New DataTable
Dim SQL As String
Dim MysqlConn As MySqlConnection
Private Sub DataIn_Load(sender As Object, e As EventArgs) Handles MyBase.Load
MysqlConn = New MySqlConnection()
'Connection String
MysqlConn.ConnectionString = "server=localhost;" _
& "user id=root;" _
& "password=123456789;" _
& "database=calendario"
Try
MysqlConn.Open()
SQL = "SELECT name FROM calendario.tables"
myCommand.CommandText = SQL
myAdapter.SelectCommand = myCommand
myAdapter.Fill(myData)
ComboBox1.Items = myData
Catch myerror As MySqlException
MessageBox.Show("Connection failed: " & myerror.Message)
Finally
MysqlConn.Close()
MysqlConn.Dispose()
End Try
End Sub
End Class
I got this message:
Table 'calendario.tables' doesn't exists
What am I doing wrong?
Also I want to know how I can encrypt the connection details hard-coded in ConnectionString 'cause in the future this application must be distributed to my customers.
use query
SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_SCHEMA='dbName'
or
USE YOURDBNAME
GO
SELECT *
FROM sys.Tables
GO
and also change your ComboBox1.Items = myData to
For intcount=0 to mydata.rows.count-1
comboBox1.Items.Add(mydata.rows(intcount).item(0))
Next
hope that helps..
mysql query,
SELECT table_name, table_type, engine FROM information_schema.tables
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I've found the following VBA code to generate the `sCode string.
But how do I insert sCode into Table1.
I'm new to MS Access programming.
Private Sub Command120_Click()
Dim sCode As String
Dim i As Long
For i = 1 To Me.Qty
sCode = Format(Now(), "YYMMDDHHNNSS") & Format(i, "0000")
Next i
End Sub
At least two ways - in both I'll assume the field itself is called sCode...
1) Use DAO:
Private Sub Command120_Click()
Dim RS AS DAO.Recordset, sCode As String, i As Long
Set RS = CurrentDb.OpenRecordset("Table1")
For i = 1 To Me.Qty
sCode = Format(Now(), "YYMMDDHHNNSS") & Format(i, "0000")
RS.AddNew
RS!sCode = sCode
RS.Update
Next i
End Sub
2) Use an SQL statement:
Private Sub Command120_Click()
Dim DB AS DAO.Database, sCode As String, i As Long
Set DB = CurrentDb
For i = 1 To Me.Qty
sCode = Format(Now(), "YYMMDDHHNNSS") & Format(i, "0000")
DB.Execute("INSERT INTO Table1 (sCode) VALUES ('" + sCode + "')");
Next i
End Sub
You may also want to wrap things up in a transaction if you want to be sure none rather than some of the updates will go through when there is an error.